简体   繁体   中英

Selenium cssSelector works in IDE but not in Webdriver

I have a web system I am automating using Java/Selenium Webdriver . I have an item I am trying to get access to. It has a compound class name. I have tried all the solutions I have been able to find here and so far none of them work.

The most offered solution looks like this:

By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());

When I check the size of the elements array it is always zero.

What I am finding however is that when I put the selector string in to the Selenium IDE (2.9.1) and use the "Find" button it identifies the correct web element without any problem at all.

I am at a loss for why it works in the IDE but not in my code.

Try selecting the element using its XPath? In the past when I ran into issues trying to select something using cssSelector, I often had success when I tried its XPath instead.

Give some wait time before the selector you are taking.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());

or try to find the elements with the help of Xpath or id.

List<WebElement> elements = driver.findElements(By.xpath("your xpath"));

Hope it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM