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 Answers 2
Try using
//li[@class='moreGenres']/span
as an xpath
For css, what you have -
.moreGenres>span
does work -
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.
-
1Thanks. 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– seleniumappiumnewbie2016年01月06日 06:32:44 +00:00Commented Jan 6, 2016 at 6:32
-
Sorry, I don't use eclipse. Maybe post that as a separate question?Michael Durrant– Michael Durrant2016年01月06日 11:32:46 +00:00Commented 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.seleniumappiumnewbie– seleniumappiumnewbie2016年01月06日 16:17:30 +00:00Commented Jan 6, 2016 at 16:17
U can try this below code, hope it helps u :
xpath("//li[contains(@class, 'moreGenres')]")
-
2Could you add more information, please? We prefer answers to be more than just a one liner with a code snippet.Kate Paulk– Kate Paulk2016年01月06日 17:25:16 +00:00Commented Jan 6, 2016 at 17:25
-
Can you replace U with you so it is written in proper English?dzieciou– dzieciou2016年01月08日 06:20:12 +00:00Commented Jan 8, 2016 at 6:20
Explore related questions
See similar questions with these tags.