I want to click a href link, then go back to previous page, then click next href link which has text "Meer info".
I wrote following code, but it is producing an Exception. I can go to web then click first link that I want to click with text name "Meer info" than go to back but then I want to click second linkText also with the same text name "Meer info" but I don't know how to do that.
WebDriver driver = new FirefoxDriver();
driver.get("http://turksegids.nl/index.php");
Thread.sleep(3000);
driver.findElement(By.cssSelector("input[type='image'][value='Zoek!']")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Meer info")).click();
Thread.sleep(4000);
driver.findElement(By.linkText("Terug naar de resultatenpagina")).click();
Thread.sleep(8000);
driver.findElement(By.linkText("//a[text()=\"Meer info\"]/following-
sibling::a[2]")).click();
Thread.sleep(4000);
driver.close();
driver.quit();
I am getting this Exception
Unable to locate element: {"method":"link text","selector":"//a[text()=\"Meer info\"]/following-sibling::a[2]"}
You can use below code:
WebDriver driver = new FirefoxDriver();
driver.get("http://turksegids.nl/index.php");
driver.findElement(By.cssSelector("input[type='image'][value='Zoek!']")).click();
driver.findElement(By.linkText("Meer info")).click();
driver.findElement(By.linkText("Terug naar de resultatenpagina")).click();
driver.findElement(By.xpath("//*[#id='resultatenklik_28957_info']/u")).click();
driver.close();
driver.quit();
and see it works or not
Try to use
List<WebElement> element = driver.findElements(By.linkText("Meer info"));
element.get(2).click();
instead of
driver.findElement(By.linkText("//a[text()=\"Meer info\"]/following-
sibling::a[2]")).click();
It will work !!
Related
I want to search Prime Video on Google Home Page and then I want to click on News Link on Google Search Page. I have used xpath to find this Link but while executing the code, I am getting NoSuchElementException. I have used below code, Please help me to know that why below code is not working ::
System.setProperty("webdriver.gecko.driver", "C:/Users/gecko/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
WebElement ele = driver.findElement(By.name("q"));
ele.sendKeys("prime video");
ele.submit();
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement news = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='hdtb-msb-vis']//div[text()='News']")));
news.click();
driver.close();
Can you try with this one. I see you forgot to Click(); as well.
Unlocate element has a several causes. One of this cause is xpath is invalid or not found on that page. One way to check is using find.element then use your xpath if it does not found it will throw an exception. Here is an example.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
/*wait page for 2 seconds -- simple way wait, but don't recommended for using real testing*/
Thread.sleep(2000);
driver.findElement(By.name("q")).Click;
driver.sendKeys("prime video");
driver.sendKeys(Keys.ENTER);
then try to verify if xpath is valid or invalid by using
try
{
driver.findElement(By.xpath("//*[#id='hdtb-msb-vis']//div[text()='News']")).Click;
}
catch(NoSuchElementException ex)
{
System.out.println("There is no element in this page or xpath is invalid : "+ex.Message);
}
catch(Exception ex)
{
System.out.println("Exception : "+ex.Message);
}
If xpath is invalid or not found, you may try Katalon Recorder or Chropath extension for chrome to let help to find xpath.
Katalon Record
https://chrome.google.com/webstore/detail/katalon-recorder/ljdobmomdgdljniojadhoplhkpialdid
Chropath
https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo?hl=en
It works for me with a slightly different xpath:
WebElement news = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"hdtb-msb-vis\"]/div[2]/a")));
webDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[1]/section[1]/div/div/a/button")).submit();
'View positions' button is not clicking with the above code.What is happening in the web page?
You see the HTML for this page is
So, you can use the CSS selector for this as
WebDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.cssSelector("button.button.teal").click();
And then proceed with doing whatever is necessary. I executed with this in my Python code and it works fine.
Also, you will need to provide the Gecko executable path while calling for the FirefoxDriver()
The way I have done it before is to use the click handler.
driver.findElement(By.cssSelector(".profile-actions .primary_button > span")).click();
I'm sure you could also select the element by xpath rather than CSS in the above line. It's a similar question to this one.
I am writing a script to locate "login" and click on it for an web based application but I am getting exception:
no such element: Unable to locate element
My code:
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wayn.com"); //to find login
driver.findElement(By.xpath("//*[#id='TopMenu']/div[1]/div/div[2]/login-buttons/div/div[1]/div[1]"))
.sendKeys(Keys.ENTER);
Try this way.
driver.get("http://www2.wayn.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='button big left red ng-isolate-scope'][#wayn-log-click='loginButtonsLogClick']")).click();
Explanation of xpath: Use class and wayn-log-click attribute along with <div> tag.
Suggestion :- Instead of using absolute xpath, use relative xpath.
try waiting a little for element to be visible:
WebDriverWait wait =new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
My question is simple- Using Selenium, how do you keep on clicking links when each hyper link opens up in a new page or new window or opens in the same web page.
For example I have following links on a webpage:
Log in
Sign up
Forgot Password?
Signup with us
follow this link
Home
Terms
Privacy Policy
Here is the small snippet of code that I have written to click these mentioned links on webpage:
List<WebElement> elements = driver.findElements(By.tagName("a"));
//clicking all links
for (WebElement el : elements){
System.out.println("Link getting clicked" + el.getText());
el.click();
driver.navigate().back();
}
As you can see I am trying to get links and trying to click them one by one. However, I am getting an error after the first click itself. Console says: "org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM".
I am petty sure I know the cause as the links are getting opened on the same webpage, but I am doing a back navigation, which is not helping me at all.
Any thoughts / suggestions?
This could be due to program execution advancing to the call to "driver.navigate().back();" before the page has loaded.
Try introducing an implicit wait, which tells the "WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available"
e.g. When you create your web driver try:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
You have to refind the element whenever a page is reloaded before you can interact with it, for your code, please try modify them to this:
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
List<WebElement> elements = driver.findElements(By.tagName("a"));
//clicking all links
for (int i=0; i<elements.size(); i++){
WebElement el = driver.findElements(By.tagName("a")).get(i);
System.out.println("Link getting clicked" + el.getText());
el.click();
driver.navigate().back();
}
I am working on a sample assignment on Thomson Holidays website (http://www.thomson.co.uk/holidays.html). On left hand side there is a Holiday Search panel. I am unable to recognize any of these elements in WebDriver. However, in IDE these elements are recognized. Need more info on this as it is the first time i am experiencing such an issue. Below is the code sample:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();
Thread.sleep(5000);
Because they're in the iframe, you need switch to the iframe first.
Two lines added to your existing code as follows:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");
// optional, unnecessary in your case
// driver.switchTo().defaultContent(); // make sure outside of all iframes
// switch to search frame
WebElement searchFrame = driver.findElement(By.cssSelector("iframe[src='/thomson/page/byo/search/usp.page']"));
driver.switchTo().frame(searchFrame);
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();
The search panel is inside an iframe. As the iframe is the first iframe, you can use the below code.
driver.get("http://www.thomson.co.uk/holidays.html");
// switch to search frame
driver.switchTo().frame(0);
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();