使用 Selenium 在 <li> 个标签内查找项目
Find items within <li> tags using Selenium
我试图在 'li' 标记使用 Selenium,但此消息发生错误:
no such element: Unable to locate element
如何在 'li' 标签元素中找到项目?
elms = driver.find_element(by=By.CLASS_NAME, value='product-list')
或
elms = driver.find_element(by=By.XPATH, value="//*[@id='product-list']")
网络资源:
<div class='product'>
<div class='menuList1'> Menu1 Menu2</div>
<div class='menuList2'> aaa </div>
<ul id='productDetail' class='product-list'>
<li class='product-list detail' id='1'> Item1</li>
<li class='product-list detail' id='2'> Item2</li>
<li class='product-list detail' id='3'> Item3</li>
<li class='product-list detail' id='4'> Item4</li>
</ul>
</div>
您可以使用元素列表:
elements = driver.find_elements(by=By.XPATH, value="//li[@class='product-list detail']")
for e in elements:
print(e.text)
你试过的有点容易出错。所需元素在 <li>
内,具有 class 属性 product-list detail
<div class='product'>
<div class='menuList1'> Menu1 Menu2</div>
<div class='menuList2'> aaa </div>
<ul id='productDetail' class='product-list'>
<li class='product-list detail' id='1'> Item1</li>
<li class='product-list detail' id='2'> Item2</li>
<li class='product-list detail' id='3'> Item3</li>
<li class='product-list detail' id='4'> Item4</li>
</ul>
</div>
解决方案
要查找 <li>
标签中的元素,您可以使用以下任一方法 :
使用css_selector:
elms = driver.find_elements(By.CSS_SELECTOR, "div.product div.menuList2 > ul.product-list li.product-list.detail")
使用 xpath:
elms = driver.find_elements(By.XPATH, "//div[@class='product']//div[@class='menuList2']/ul[@class='product-list']//li[@class='product-list detail']")
我试图在 'li' 标记使用 Selenium,但此消息发生错误:
no such element: Unable to locate element
如何在 'li' 标签元素中找到项目?
elms = driver.find_element(by=By.CLASS_NAME, value='product-list')
或
elms = driver.find_element(by=By.XPATH, value="//*[@id='product-list']")
网络资源:
<div class='product'>
<div class='menuList1'> Menu1 Menu2</div>
<div class='menuList2'> aaa </div>
<ul id='productDetail' class='product-list'>
<li class='product-list detail' id='1'> Item1</li>
<li class='product-list detail' id='2'> Item2</li>
<li class='product-list detail' id='3'> Item3</li>
<li class='product-list detail' id='4'> Item4</li>
</ul>
</div>
您可以使用元素列表:
elements = driver.find_elements(by=By.XPATH, value="//li[@class='product-list detail']")
for e in elements:
print(e.text)
你试过的<li>
内,具有 class 属性 product-list detail
<div class='product'>
<div class='menuList1'> Menu1 Menu2</div>
<div class='menuList2'> aaa </div>
<ul id='productDetail' class='product-list'>
<li class='product-list detail' id='1'> Item1</li>
<li class='product-list detail' id='2'> Item2</li>
<li class='product-list detail' id='3'> Item3</li>
<li class='product-list detail' id='4'> Item4</li>
</ul>
</div>
解决方案
要查找 <li>
标签中的元素,您可以使用以下任一方法
使用css_selector:
elms = driver.find_elements(By.CSS_SELECTOR, "div.product div.menuList2 > ul.product-list li.product-list.detail")
使用 xpath:
elms = driver.find_elements(By.XPATH, "//div[@class='product']//div[@class='menuList2']/ul[@class='product-list']//li[@class='product-list detail']")