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();
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Jan 6, 2016 at 0:20

2 Answers 2

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.

answered Jan 6, 2016 at 1:32
3
  • 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? Commented Jan 6, 2016 at 6:32
  • Sorry, I don't use eclipse. Maybe post that as a separate question? Commented Jan 6, 2016 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. Commented Jan 6, 2016 at 16:17
0

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

xpath("//li[contains(@class, 'moreGenres')]")
demouser123
3,5325 gold badges30 silver badges41 bronze badges
answered Jan 6, 2016 at 15:19
2
  • 2
    Could you add more information, please? We prefer answers to be more than just a one liner with a code snippet. Commented Jan 6, 2016 at 17:25
  • Can you replace U with you so it is written in proper English? Commented Jan 8, 2016 at 6:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.