Anik
Anik

Reputation: 1

Selenium: click on <li>

I have the following html code and I want to click on the second option "Plan b".

<ul class="clearfix all">
<li data-content="a" data-tab-target="tab1" class="active"><span>Plan a</span</li>
<li data-content="b" data-tab-target="tab2" class=""><span>Plan b</span></li>
<li data-content="c" data-tab-target="tab3" class=""><span>Plan c</span></li>/ul>

I tried to do somthing like this:

1.

@FindBy(css = "li:nth-child(2)")
WebElement sparpreis;
sparpreis.click();

2.

@FindBy(xpath = "//*[@class='clearfix all']/ul/li[2]")
WebElement sparpreis;
sparpreis.click();

My error messages:

  1. org.openqa.selenium.ElementNotVisibleException: element not visible

  2. org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='clearfix all']/ul/li[2]"}

Perhaphs I should activate the second "class"?

Upvotes: 1

Views: 4902

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193298

As per the HTML you have shared, to click on the second option Plan b you can use either of the following code block :

  • css

    @FindBy(css = "ul.clearfix.all li[data-content=b] > span")
    WebElement sparpreis;
    sparpreis.click();
    
  • xpath

    @FindBy(xpath = "//ul[@class='clearfix all']//li[@data-content='b']/span")
    WebElement sparpreis;
    sparpreis.click();
    

Upvotes: 0

Danny
Danny

Reputation: 287

The /ul is not needed, try the following:

@FindBy(xpath = "//ul[contains(@class, 'clearfix all')]/li[2]")

Upvotes: 1

Related Questions