admin管理员组

文章数量:1318986

I am currently trying Selenium Webdriver on the amazon.es webpage. I have successfully navigated through the webpage, looking for an element by class (the search item button to be precise).

The problem is when I try to get the prices of the webpage. The webpage can be found here (just look for Balon Baloncesto=> Basketball). When I try to search the element by class, this one exactly:

<span class="a-size-base a-color-price s-price a-text-bold">EUR 33,64</span>

Selenium cannot find it, giving the not found element error. Here is the Java implementation. nav is the browser object.

//value = a-size-base a-color-price s-price a-text-bold
elemList = nav.findElements(By.className(value));
//elemList appears to be empty after this

There are not any iFrames on the webpage which can affect me, so I do not get very well where can the problem e from. Any help would be appreciate it.

Best regards

I am currently trying Selenium Webdriver on the amazon.es webpage. I have successfully navigated through the webpage, looking for an element by class (the search item button to be precise).

The problem is when I try to get the prices of the webpage. The webpage can be found here (just look for Balon Baloncesto=> Basketball). When I try to search the element by class, this one exactly:

<span class="a-size-base a-color-price s-price a-text-bold">EUR 33,64</span>

Selenium cannot find it, giving the not found element error. Here is the Java implementation. nav is the browser object.

//value = a-size-base a-color-price s-price a-text-bold
elemList = nav.findElements(By.className(value));
//elemList appears to be empty after this

There are not any iFrames on the webpage which can affect me, so I do not get very well where can the problem e from. Any help would be appreciate it.

Best regards

Share Improve this question edited Apr 2, 2018 at 17:21 Doron Yakovlev Golani 5,48010 gold badges41 silver badges63 bronze badges asked Apr 2, 2018 at 14:57 J. BaronJ. Baron 252 silver badges6 bronze badges 2
  • Please post the actual error message. I'm guessing it's not element not found if you are using the entire class inside By.className(). If you did that, you would get something along the lines of "Compound class names not permitted" which is basically stating that you are searching for more than one classname inside of a locator that takes only one classname. – JeffC Commented Apr 2, 2018 at 18:43
  • The error what I get is element not found, not the other one that you mention – J. Baron Commented Apr 3, 2018 at 7:41
Add a ment  | 

2 Answers 2

Reset to default 6

By.className(value) has specific behavior for elements with multiple classes. I can't find this behavior documented in the JS bindings, but here's an excerpt from the same functionality in Java.

From the docs:

If an element has multiple classes, then this will match against each of them. For example, if the value is "one two onone", then the class names "one" and "two" will match.

So when the actual class attribute is class="a-size-base a-color-price s-price a-text-bold" only the following By.className identifiers would match.

By.className("a-size-base")
By.className("a-color-price")
By.className("s-price")
By.className("a-text-bold")

Be warned however that I would expect one or more of these to additionally identify some other objects on the screen that you don't want.

If you want to match on ALL of the classes, try using an xpath which checks if the @class attribute contains your value.

Alternatively (and this will typically perform better than xpath) use a css selector that chains all of the class names together. Each class name in a CSS selector should be preceded by a .

By.css(".a-size-base.a-color-price.s-price.a-text-bold")

This code might help you:

WebDriver nav = new FirefoxDriver();
nav.get("https://www.amazon.es/s/ref=nb_sb_noss/261-8047486-3232562?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&url=search-alias%3Daps&field-keywords=Balon+Baloncesto&rh=i%3Aaps%2Ck%3ABalon+Baloncesto");

 List<WebElement> elements = nav.findElements(By.xpath("//span[contains(@class,'a-size-base a-color-price s-price a-text-bold')]"));
 for(WebElement element: elements) {
         System.out.println(element.getText());
  }

本文标签: javascriptCan39t find element by class selenium amazonStack Overflow