How to Click an Li Inside a Ul Using Selenium Webdriver

How do I click an li inside a ul using Selenium Webdriver

If you want to click on the first link, meaning

 update My details 

then use this:

driver.findElement(By.cssSelector("li:nth-child(1).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();

If you want to click on the second link, meaning

 Book a Holiday 

then use this:

driver.findElement(By.cssSelector("li:nth-child(2).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();

If you want to click on the third link, then use this:

driver.findElement(By.cssSelector("li:nth-child(3).NoBullet.jms-bullet> h3>a[href^='javascript:ShowSelectedAddInScreen']")).click();

This can be simplified by using the below code:

//first create a web element list which contains all elements inside a list:

List<WebElement> elems = driver.findElements(By.cssSelector("ul.NoIndent>li.NoBullet.jms-bullet> h3>a"));

//Now you can select individual elements from a list using:

elems.get(0).click();//for the 1st element
elems.get(1).click();//for the 2nd element
elems.get(2).click();//for the 3rd element

How do I click an li inside a ul using Selenium in python?

The element is avaible inside an iframe.You have to switch to iframe first.Then click on the link National Data.

Induce WebDriverWait and frame_to_be_available_and_switch_to_it()
Induce WebDriverWait and element_to_be_clickable() and following XPATH

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://dataunodc.un.org/GSH_app")
WebDriverWait(driver,15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://unodc.shinyapps.io/GSH_App/']")))
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="nav navbar-nav"]//a[text()="National Data"]'))).click()

OR You can use following CSS selector.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://dataunodc.un.org/GSH_app")
WebDriverWait(driver,15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='https://unodc.shinyapps.io/GSH_App/']")))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'ul.nav.navbar-nav a[data-value="National Data"]'))).click()

Browser snapshot:
enter image description here


To click on all checkbox try this code.

allchekbox=WebDriverWait(driver,5).until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@name='YearVar' and not(@checked='checked')]")))
print(len(allchekbox))

for item in allchekbox:
item.click()

How do I click the li tag inside ul with Selenium?

finally i found solution from link

this is part of my code, how to solve element not visible exception when click li

 WebElement room_select1= driver.findElement(By.xpath("(//input[@type='text'])[2]"));
(( JavascriptExecutor)driver).executeScript("arguments[0].click();", room_select1);
int li_size=driver.findElements(By.xpath("//a[@ref='1']")).size();
driver.findElements(By.xpath("//a[@ref='1']")).get(li_size-1).click();

How to select an item (li) within a list (ul) through Selenium WebDriver and C# - timeout when locating

As it turns out, the solution was very simple and not what pops into mind...
All I had to do is update my ChromeDriver.exe to the latest version (it was 8 months old) - and I was able to find all elements that caused timeout before.

I've updated some Selenium packages in my Visual Studio not so long ago which most likely caused this situation, but failed to remember that the ChromeDriver should be updated accordingly.

Webdriver Selenium click a <li> element inside an Iframe

I would advise you to use explicit wait to switch to frame and interact with them :

to click on second li tag, below code should work for you.

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "moduleManagement")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#colorbox+div[class^='daterangepicker']"))).click()
second_li_element = wait.until(EC.visibility_of_element_located((By.XPATH, "(//li[@data-range-key])[2]")))
second_li_element.click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click on a descendant li element with Selenium?

I figured it out, I had to have the drop down open to click on them but the dropdown would immediately close when selenium clicked on it, all I had to do was make selenium click on it twice and that fixed the issue.

Unable to click link in li tag in selenium

You can click on the menu based on the text present in the menu. Here is some samples,

Search Menu item--

driver.findElement(By.xpath("//a[contains(text(),'Search')]//parent::li")).click();

Member menu item --

driver.findElement(By.xpath("//a[contains(text(),'Member')]//parent::li")).click();

Selenium: click on <li>

The /ul is not needed, try the following:

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


Related Topics



Leave a reply



Submit