简体   繁体   English

如何使用 Selenium 和 Python 定位后代标签元素

[英]How to locate the descendant label elements using Selenium and Python

I'm using Selenium python to try to find out all the descendant under the first div, so I used this code:我正在使用 Selenium python 尝试找出第一个 div 下的所有后代,所以我使用了以下代码:

label_element =driver.find_elements_by_xpath("//div[@style='display:block']/descendant::label")

But get an empty list [].但是得到一个空列表[]。

<div id="coption5" class="copt" style="display: block;">
<div style="height:100%;display:flex;align-items:center;justify-content:center;">
<div class="coptw">
<div style="width:100%;height:49px;border-bottom:1px solid #888">
<b class="cpopdish">SUPREME CALZONE (M)  10.99</b>
<b class="cpopmodifi gray" data-iid="0" style="font-weight: normal;">
<i class="fa fa-comments-o"></i> Special Request</b><b class="cpopprice">10.99</b></div>
<div class="comain" style="right: 0px;">
<div class="crow" grp="0" grpname="">

<label class="label0" cid="5" style="">
<input type="radio" name="0" coname="BF PEPPERONI(M)" sname="" price="0.00" value="2">BF PEPPERONI(M)<b class="ip">0.00</b>
</label>
<label class="label0" cid="5"><input type="radio" name="0" coname="BLACK OLIVES(M)" sname="" price="0.00" value="3">BLACK OLIVES(M)<b class="ip">0.00</b>
</label>
<label class="label0" cid="5"><input type="radio" name="0" coname="CHICKEN(M)" sname="" price="1.00" value="4">CHICKEN(M)<b class="ip">1.00</b>
</label>
<div style="clear:both"></div></div>
</div><a class="ocancel" data-cid="5" data-grps="0"><i class="fa fa-remove"></i> Cancel</a></div></div>

Any friend know how to use Xpath or Css selector to locate all the label tag?哪位朋友知道如何使用Xpath或Css选择器来定位所有的label标签?

this first part of my code:我的代码的第一部分:

driver.find_elements_by_xpath("//div[@style='display:block']")

Can locate the first div element successfully so I think maybe there is nothing wrong with the visibility issues.可以成功定位第一个 div 元素,所以我认为可见性问题可能没有任何问题。 The label tag is inside the first div tag, label are the descendant of the first div. label 标签位于第一个 div 标签内,label 是第一个 div 的后代。

So any friend can help?所以有朋友可以帮忙吗?

To extract all the text items from the <label> using Selenium and Python you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies :要使用SeleniumPython<label>提取所有文本项,您必须为visibility_of_all_elements_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.copt[id^='coption'] div.comain>div.crow>label")))])
  • Using XPATH :使用XPATH

     print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='copt' and starts-with(@id, 'coption')]//div[@class='comain']/div[@class='crow']/label")))])
  • 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

Here is the correct xpath that will find all 3 labels.这是将找到所有 3 个标签的正确 xpath。

//div[@style='display: block;']/descendant::label

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM