Click link selenium web driver works, i.e. not firefox

I can’t understand what’s going on for life. the code is simple:

//WebDriver driver = new InternetExplorerDriver(); //WebDriver driver = new FirefoxDriver(); driver.get("http://www.yahoo.com"); driver.findElement(By.xpath("//*[@id='pa-u_14782488-bd']/a/span[2]")).click(); 

I use either ff or ie driver. but the last two lines of code are the same. works, i.e., but not ff. funny that i get xpath from fb firebug, so xpath is correct for ff. ff version 7.0.1. Its just a Mail link in the left column of the yahoos site. Why is it so hard?

+6
source share
3 answers

As Slanec said, these types of sites use dynamic identifiers, so the best option in the above case would be to use the "title" attribute, which is less likely to change. if you want to go with xpath this will work,

 driver.findElement(By.xpath("//*[@title='Mail']")).click(); 

It’s even better to use link text , because it works the same way a user manually clicks ...

 driver.findElement(By.linkText("MAIL")).click(); 
+2
source

Selenium sometimes encounters elements embedded in link elements ( <a> ). Try using your code without the last part of XPath. So:

 //*[@id='pa-u_14782488-bd']/a 

Make sure you also publish which version of Selenium you are using so that others can give you more detailed help.

0
source

Do not use xpath search .. its very erratic and does not help your business at all. Also, your selenium code is now closely linked to the markup, and any changes to the markup, such as introducing a container (like a div), will not pass the test.

you can use scope to achieve something like this. Example:

 var container=driver.findElement(By.xpath("//*[@id='pa-u_14782488-bd']")); var spans=container.findElements(By.tagName("span")); spans[1].Click(); 

And I don’t understand how this works because you have a link, and since I guess you have two spaces in it for style purposes, but clicking on any of them should still trigger the same action as and click on the correction button? or am I missing something?

It is possible that firefox is not reporting your events correctly.

0
source

Source: https://habr.com/ru/post/901680/


All Articles