Webdriver is able to navigate to the base url but I am unable to perform any actions in it like clicking a link or getting the page title. Even the:
driver.getCurrentUrl()
is throwing:
unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"
Not sure how to fix it.
Can you check if you have prepended http in your URL. I think that will do to solve your issue in case you did not use it.
Change the navigate part as:
driver.navigate().to("http://www.example.com");
in your second ex too, use the following:
driver.navigate().to("http://www.url.com/");
driver.findElement(By.linkText("Login").click();
Complete source code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;
public class CheckWebDriver {
private final WebDriver driver = new SafariDriver();
void checkLogin(){
String baseUrl = "http://newtours.demoaut.com/";
driver.get(baseUrl);
System.out.println(driver.getCurrentUrl());
driver.findElement(By.linkText("REGISTER")).click();
}
public static void main(String[] args) {
CheckWebDriver web = new CheckWebDriver();
web.checkLogin();
}
}
Related
I'm tring to send the login credentials within the username and password field https://www.vignanits.ac.in/server/moodle/login/index.php which needed to be automated using HtmlUnitDriver but facing NoSuchElementException.
Code trials:
package leela;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.WebClient;
public class LeelaHtmlUnit {
public static void main(String[] args) throws InterruptedException {
HtmlUnitDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
System.out.println(driver.getCurrentUrl()+driver.getTitle());
}
}
enter code here
Ideally, to use htmlunit-driver you need to download htmlunit-driver-2.42.0-jar-with-dependencies.jar the latest release as of today.
The below code block works perfect at my end:
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HtmlUnitDriverDemo {
public static void main(String[] args) {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.get("https://www.vignanits.ac.in/server/moodle/login/index.php");
System.out.println(driver.getCurrentUrl()+driver.getTitle());
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("username")));
driver.findElement(By.id("password")).sendKeys("xyz");
driver.findElement(By.id("loginbtn")).click();
}
}
Console Output:
https://www.vignanits.ac.in/server/moodle/login/index.phpVIGNAN MOODLE: Log in to the site
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element
package automation;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MainPage {
private final WebDriver driver;
public MainPage(WebDriver driver) {
this.driver = driver;
}
public MainPage loginAs(String username, String password) {
driver.get("URL");
driver.findElement(By.name("email")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.className("login")).click();
return new MainPage(driver);
}
public static void main(String[] args) {
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
MainPage login = new MainPage(new ChromeDriver());
login.loginAs("qa#gmail.com", "123456");
}
}
I tried to test login function for the web site and wrote above code for it. However,
I couldnt find the error on it. Can someone help me to figure it out?
I see a few things wrong with your code:
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
Should be (make sure the chromedriver is the correct file, the windows version usually has an .exe extension.)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
You also should navigate the url of the page you want to test using
WebDriver driver = new ChromeDriver();
driver.get("http://[enter url here]")
Please write the "gecko driver" instead of "genko driver".
The website I am trying to automate is betting website and I have a scenario to automate a horse betting.
I am using selenium 3.0 with Java
From the site I am able to travel to horse race but unable to select Tomorrow and select the race. I tried using xpath, class and other methods but unable to click on these button.
website is
https://www.williamhill.com.au/
1 step. go to the above url
2. Select horse racing from top left corner or navigate to url (https://www.williamhill.com.au/racing?event=horseracing)
3.Click on Tomorrow I am unable to do this
4. Select on particular race from the table (unable to this too)
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class horseRacing {
public static void main(String[] args) throws Exception {
String exePath = "D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("https://www.williamhill.com.au/");
Thread.sleep(5);
driver.manage().window().maximize();
String Title = driver.getTitle();
System.out.println(Title);
driver.findElement(By.className("MenuItem_text_N8V")).click();
Thread.sleep(25);
// driver.findElement(By.className("RaceGrid_raceTile_imG RaceGrid_raceDisabled_Q0m")).click();
driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
// driver.quit();
}
}
I tried to click "TOMORROW" and succeeded.
package test;
import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateToAUrl {
#Test
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.williamhill.com.au/racing?event=horseracing");
driver.findElement(By.xpath(".//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
}
}
Dot(.) is missing in your code driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
(Before //*[#id~)
I'm trying to follow the Selenium Webdrive Basic Tutorial in the case of using HTML tables here
http://www.toolsqa.com/selenium-webdriver/handling-tables-selenium-webdriver/
The code of "Practice Exercise 1" in that page doesn't work: the problem seems to be about the xpath filter here
String sCellValue = driver.findElement(By.xpath(".//*[#id='post-1715']/div/div/div/table/tbody/tr[1]/td[2]")).getText();
and here
driver.findElement(By.xpath(".//*[#id='post-1715']/div/div/div/table/tbody/tr[1]/td[6]/a")).click();
The page used in the sample code is this one
http://www.toolsqa.com/automation-practice-table/
I've tried to change the code extracting the xpath directly form the page using Firebug and my new code is the following
package practiceTestCases;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PracticeTables_00 {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/automation-practice-table");
//Here we are storing the value from the cell in to the string variable
String sCellValue = driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]")).getText();
System.out.println(sCellValue);
// Here we are clicking on the link of first row and the last column
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[6]/a")).click();
System.out.println("Link has been clicked otherwise an exception would have thrown");
driver.close();
}
}
Trying to execute the error is still
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[2]/div/div/table/tbody/tr[1]/td[2]"}
I'm using Eclipse Luna on Windows 7
Any suggestions? Thank you in advance ...
Cesare
Your xpath is wrong. As I see you try to get the content from the second row/first column (if you doesn't count the headers). Try the following code on http://www.toolsqa.com/automation-practice-table/ page:
/html/body/div[2]/div[3]/div[2]/div/div/table/tbody/tr[2]/td[1]
or
//*[#id='content']/table/tbody/tr[2]/td[1]
If you run getText() it will return the following value: Saudi Arabia
As they given in Practice Test scenario, that xpath has been changed. And ID or className in the xpath might be changed. so change the xpath as per updated HTML code.
Here i have changed everything:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PracticeTables_00 {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/automation-practice-table");
// Here we are storing the value from the cell in to the string variable
String sCellValue = driver
.findElement(
By.xpath(".//*[#id='content']/table/tbody/tr[1]/td[2]"))
.getText();
System.out.println(sCellValue);
// Here we are clicking on the link of first row and the last column
driver.findElement(
By.xpath(".//*[#id='content']/table/tbody/tr[1]/td[6]/a"))
.click();
System.out
.println("Link has been clicked otherwise an exception would have thrown");
driver.close();
}
}
i have written a basic test using selenium. The test is failing as the selenium unable to select the item from drop down list.
My select statement is not getting executed. Also i have include this select statement within iframe as this falls under it.
Can you please let me know what is the issue with my select statement.
Here is my code for southwest website:
package Default;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstWDWithoutRecording {
#Test
public void SouthWestSignUp() throws InterruptedException
{
//Open the FF/Chrome browser
//FirefoxDriver oBrw = new FirefoxDriver();
ChromeDriver oBrw = new ChromeDriver();
//Maximize Browser
oBrw.manage().window().maximize();
//Open/Launch www.southwest.com
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
oBrw.get("http://www.southwest.com/");
//Click on Sign up and Save
//Recognising
oBrw.findElement(By.linkText("Sign up")).click();
//oBrw.get("http://www.southwest.com/html/email/click_n_save_signup.html?clk=GFOOTER-CNS-ENROLL");
Thread.sleep(5000);
//Enter First Name
oBrw.switchTo().frame(0); //'0' as it is the only iframe on the page, the value is the index of all iframes on the page
//do your login actions
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).clear();
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).sendKeys("abc");
//Enter Last Name
oBrw.findElement(By.id("LAST_NAME")).clear();
oBrw.findElement(By.id("LAST_NAME")).sendKeys("Kish123");
//Enter Email ID
oBrw.findElement(By.id("EMAIL")).clear();
oBrw.findElement(By.id("EMAIL")).sendKeys("abc#Kish123.com");
//Selecting Home Airport
Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT")));
uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");
//Accepting Conditions
oBrw.findElement(By.id("IAN")).click();
//Click Submit
oBrw.findElement(By.id("submit")).click();
//after return
oBrw.switchTo().defaultContent();
}
}
As #SiKing had stated, your issue is with your deselectByVisibleText(). You want selectByVisibleText().
uiHomeAp.selectByVisibleText("Atlanta, GA - ATL");
Also wanted to point out that it might be easier if you are just getting started, to check out the getting started with selenium framework. If you're using maven, you could do:
<dependency>
<groupId>io.ddavison</groupId>
<artifactId>getting-started-with-selenium-framework</artifactId>
<version>1.2</version>
</dependency>
then your test would look like this:
#Config(browser = Browser.CHROME, url="http://www.southwest.com/")
public class FirstWDWithoutRecording extends AutomationTest {
public void southWestSignUp() {
click(By.linkText("Sign up"))
.switchToFrame(0)
.setText("input#FIRST_NAME", "abc")
.setText("input#LAST_NAME", "Kish123")
.setText("input#EMAIL", "abc#Kish123.com")
.selectOptionByText("select#HOME_AIRPORT", "Atlanta, GA - ATL")
.check("#IAN") // check the terms and conditions
.click("#submit") // form submitted
.switchToDefaultContent()
;
}
}