huan le
huan le

Reputation: 263

Selenium: chromedriver can not find element on Polymer website


I have automation project use Selenium, it will call Chrome webdriver and run testcase.

But Chromedriver cann't element on website build by Polymer framework,
we can view element by check F12. but chromedriver cannot find element.

Ex: here url of site https://shop.polymer-project.org/ we can't use ChomeDriver to find element inner node "shadow-root". here code:

System.setProperty("webdriver.chrome.driver", "chromedriver_win_2.23.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://shop.polymer-project.org/");
Thread.sleep(5000);

System.out.println(driver.getTitle());
System.out.println(driver.getPageSource());
WebElement shopnowbtn = driver.findElement(By.xpath("//shop-button/a"));
shopnowbtn.click();

Here is error

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='style-scope shop-home']/a"} (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.04 seconds

some my infor:Chrome Version 52.0.2743.116 m,Selenium-server-standalone version 2.53, ChromeDriver version 2.23
I have used FirefoxDriver is working fine. i user Firefox version 4.6

So are there anyway to run automation script by chromedriver for Polymer website ?
Many Thanks

Upvotes: 1

Views: 927

Answers (2)

lauda
lauda

Reputation: 4173

The element can not be found because there are some differences between browsers.

You should check manually the selector in both browser in situation like this.

If you will inspect on chrome browser you will see that class "style-scope shop-home" is missing.

I recommend using a selector based on href. Here is a variant of the selector for xpath and css.

//shop-button/a[contains(@href, 'list')]

or

shop-button a[href*=list]

When you know the page can be different from browser to browser always check the selector in both browsers.

Upvotes: 0

Grasshopper
Grasshopper

Reputation: 9058

As I cannot use comment as I am a newbie here, adding to @lauda answer, Polymer uses Shadow DOM. But there are differences between the resulting page source of the web page for Firefox and Chrome for styling. Sometimes the document level CSS override the local styling of the Shadow DOM elements. Even the appearance of the 'Shop Now' element is different in the two browsers.

As suggested earlier you will need to modify your locators to remove class values. Maybe add ids to the elements you want.

Upvotes: 2

Related Questions