I want to access <li>
items in a webpage.
From the given HTML, how can I access the list items such as User, Make & Model??
I am not able to retrieve the content of the list. My code is not executing the codes added inside the for loop.
HTML:
<li class="nav-item"> <span class="nav-link add-items" data-toggle="collapse" data-target="#add"> <i class="fas fa-plus"></i> Add</span>
<ul class="add-menu collapse" id="add">
<li><span data-toggle="modal" data-target="#add-user-modal">User</span></li>
<li><span data-toggle="modal" data-target="#add-make-modal">Make</span></li>
<li><span data-toggle="modal" data-target="#add-model-modal">Model</span></li>
</ul>
</li>
I want to access <li>
items in a webpage.
From the given HTML, how can I access the list items such as User, Make & Model??
I am not able to retrieve the content of the list. My code is not executing the codes added inside the for loop.
HTML:
<li class="nav-item"> <span class="nav-link add-items" data-toggle="collapse" data-target="#add"> <i class="fas fa-plus"></i> Add</span>
<ul class="add-menu collapse" id="add">
<li><span data-toggle="modal" data-target="#add-user-modal">User</span></li>
<li><span data-toggle="modal" data-target="#add-make-modal">Make</span></li>
<li><span data-toggle="modal" data-target="#add-model-modal">Model</span></li>
</ul>
</li>
Share
Improve this question
edited May 28, 2019 at 14:02
undetected Selenium
194k44 gold badges303 silver badges381 bronze badges
asked May 28, 2019 at 9:07
ThomasThomas
111 gold badge1 silver badge2 bronze badges
3
- Post your code in your post – Radonirina Maminiaina Commented May 28, 2019 at 9:09
- @Thomas Which language are you using Java / Python / C# / JavaScript? – undetected Selenium Commented May 28, 2019 at 13:23
- if(driver!=null) { System.out.println("Driver is not null"); List<WebElement> add= driver.findElements(By.xpath("//li/span[text()='Add']")); for(WebElement element:add) { System.out.println("for loop"); System.out.println(element.getText()); } driver.findElement(By.xpath("//*[@id=\"add\"]/li[1]/span")).click(); Thread.sleep(1000); Kindly note that I am not able to enter the loop using the above code. Also, the "Add" link is appearing with + sign and options are visible when we click the + symbol. Still, I am not able to click the "Add" – Thomas Commented Jun 9, 2019 at 5:41
2 Answers
Reset to default 1To match a single item you can use the following XPath locator :
//li/span[text()='User']
To match all items and get their text the relevant XPath Expression would be:
//ul[@class='add-menu collapse']/li/span
Example Python code:
for li in driver.find_elements_by_xpath("//ul[@class='add-menu collapse']/li/span"):
print(li.text)
To print the list items such as User, Make & Model you can use the following solution:
Java:
Using
cssSelector
:List<String> myItems = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.nav-item ul.add-menu.collapse li>span"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()); System.out.println(myItems);
Using
xpath
:List<String> myItems = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[@class='nav-item']//ul[@class='add-menu collapse']//li/span"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()); System.out.println(myItems);
Python:
Using
CSS_SELECTOR
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "li.nav-item ul.add-menu.collapse li>span")))])
Using
XPATH
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//li[@class='nav-item']//ul[@class='add-menu collapse']//li/span")))])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.mon.by import By from selenium.webdriver.support import expected_conditions as EC
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425958a4627178.html
评论列表(0条)