3

I can't find this element using selenium java webdriver.

I have a before class method that loads the firefox driver and set the WebDriverWait. Then I have a test method to look for the elements with the code below.

It never finds the following element. Can someone help me in figuring out this issue?

//Click More Genres 
<li class="moreGenres">
<span>More Genres</span>
</li>

The code:

Actions builder = new Actions(driver);  
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".moreGenres>span")));
WebElement moregenres = driver.findElement(By.cssSelector(".moreGenres>span"));
builder.moveToElement(moregenres).click(moregenres);
builder.perform();

or using:

//Click More Genres
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".moreGenres>span")));
WebElement moregenres = driver.findElement(By.cssSelector(".moreGenres>span"));
moregenres.click();

//Click More Genres
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='moreGenres']")));
WebElement moregenres = driver.findElement(By.xpath("//li[@class='moreGenres']"));
moregenres.click();

or:

//Click More Genres
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='moreGenres']/span")));
WebElement moregenres = driver.findElement(By.xpath("//li[@class='moreGenres']/span"));
moregenres.click();
2

Try using

//li[@class='moreGenres']/span

as an xpath

For css, what you have -

.moreGenres>span

does work -

enter image description here

so see if maybe you have that li in more than one place perhaps?

or maybe make it a bit more specific such as

li.moreGenres>span

or

ul>li.moreGenres>span

or (better)

ul.this_list>li.moreGenres>span

It might also be better to have style rules for the li's rather than thru adding spans perhaps.

  • 1
    Thanks. You are right. It ended up being two instances of the element. So I had to use another tag before it plus this part of the element to differentiate between the two elements. So that each instance of the element has its own unique xpath or cssSelector. However, now I have a new problem. My eclipse testng doesn't run the save code changes, it keeps remembering the previously bad runs with the failing code despite I'm making new code changes. Do you know how I can solve this? – seleniumappiumnewbie Jan 6 '16 at 6:32
  • Sorry, I don't use eclipse. Maybe post that as a separate question? – Michael Durrant Jan 6 '16 at 11:32
  • Thanks. The reason it wasn't taking the new code, was actually I still had some old xpaths that I didn't update to the new xpath in the bottom of the page code. That was why it was still running an old xpath. Now it's solved. Thanks for helping me figure out that it was two instances of the same element. That helped a lot. – seleniumappiumnewbie Jan 6 '16 at 16:17
0

U can try this below code, hope it helps u :

xpath("//li[contains(@class, 'moreGenres')]")
  • 2
    Could you add more information, please? We prefer answers to be more than just a one liner with a code snippet. – Kate Paulk Jan 6 '16 at 17:25
  • Can you replace U with you so it is written in proper English? – dzieciou Jan 8 '16 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.