问题
I want to extract all <li>
element text that are under <ul>
for which I tried
elem = driver.find_elements_by_xpath(("//div[@class='left width50']/p/b/ul"))
len(elem)
gives '0' or empty list.
here is the html source
<div class="left width50">
<p><b>Features:</b></p>
<ul>
<li>Easy spray application</li>
<li>Excellent bonding properties</li>
<li>Single package</li>
<li>Mixed with clean potable water at job site</li>
</ul>
</div>
HERE is the link of the website
How to go about it any suggestions?
回答1:
Actually you're trying to find the path after the p
and b
tag. that will look something like this.
<div class="left width50">
<p><b>Features:<ul>
<li>Easy spray application</li>
<li>Excellent bonding properties</li>
<li>Single package</li>
<li>Mixed with clean potable water at job site</li>
</ul></b></p>
</div>
But your code is different in HTML.
So you should look around without the p
and b
tag.
Here is the quick help you can take from chrome. Go to developer option using f12 key
and navigate to elements tab and then right click on the element which you want to find out and select the selector value.
You can read more about what are the ways to find the element here
If you want to use the xPath
this is right xpath for you - //*[@id="borderForGrid"]/div[1]/ul
Extraction Process
Once you'll get all the ul
this will help you to get all the li
text
all_li = all_ul_from_xpath.find_elements_by_tag_name("li")
for li in all_li:
text = li.text
print (text)
Working code for reference.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.carboline.com/products/")
elem = driver.find_element_by_xpath('//*[@id="borderForGrid"]/div[1]/ul')
all_li = elem.find_elements_by_tag_name("li")
for li in all_li:
text = li.text
print (text)
Output
回答2:
Presumably, you wanted to extract all <li>
element's text that are associated with <h5>
tag with text as A/D TC-55 SEALER and to achieve that you can use either of the following Locator Strategies:
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[data-brands='Southwest'][data-types='Acrylics'] div.left.width50 ul>li")))])
Using
XPATH
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//h5//a[text()='A/D TC-55 SEALER']//following::div[1]//ul//li")))])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
回答3:
There's is no element with the xpath :
//div[@class='left width50']/p/b/ul
left width50
has 500 web element associated with it. So does //div[@class='left width50']/p/b
That's why you are getting 0 while doing len().
Instead try replacing it with this xpath
//a[text()='A/D Firefilm III']/../following-sibling::div[1]/descendant::li
来源:https://stackoverflow.com/questions/56421690/how-to-extract-all-li-elements-under-ul