【问题标题】:How can I return an item inside a tag in python如何在python的标签内返回一个项目
【发布时间】:2021-03-02 18:15:40
【问题描述】:

我在网页上有以下代码:

<label data-correct="false">
    <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">

    <!--option image-->
    <!---->

    <!--option text-->
    <span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Earned three college degrees and supported himself as a small businessman - Google values entrepreneurship.</p></span>
</label>
<label data-correct="true">
    <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">

    <!--option image-->
    <!---->

    <!--option text-->
    <span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Has several years of work experience, and enjoys volunteering for Habitat for Humanity - Google values these character traits.</p></span>
</label>
<label data-correct="false">
    <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">

    <!--option image-->
    <!---->

    <!--option text-->
    <span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Recently graduated from Harvard University - Google is known for hiring the best and the brightest.</p></span>
</label>
<label data-correct="false">
    <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng-valid ng-empty" value="[object Object]">

    <!--option image-->
    <!---->

    <!--option text-->
    <span ng-bind-html="quizCtrl.trustAsHtml(option.text)"><p>Worked as an independent contractor in different areas of the computer industry - Google values versatility.</p></span>
</label>

我想定位数据正确的字段,如果它是真的,程序应该点击它。这是我目前所拥有的:

from selenium import webdriver

driver = webdriver.Chrome('chromedriver.exe')
driver.get(url)
driver.find_elements_by_tag_name('label')

我对 python 和 selenium 还很陌生,所以我不确定下一步该做什么

【问题讨论】:

    标签: python html python-3.x selenium


    【解决方案1】:
    data = [ i.get_attribute("data-correct") for i in driver.find_elements_by_tag_name('label')]
    

    data-correct 是 label 元素的一个属性,因此您可以使用 get_attribute 如果您能够正确定位该元素

    如果你不关心获取标签,而是点击它,那么请改用 xpath

    [ i.click for i in driver.find_elements_by_xpath('//label[@data-correct="true"]')]
    

    但是如果点击元素会导致元素过时,那么你必须重新查找所有元素。

    改用:

    elems = driver.find_elements_by_xpath('//label[@data-correct="true"]')
    
    count = 0
    
    while(count!=len(elems)):
        elems = WebDriverWait(driver,15).until(EC.presence_of_all_elements_located(By.xpath,'//label[@data-correct="true"]'))
        elems[count].click()
        count+=1;
        
    

    如果需要,请使用 webdriver 等待:

     from selenium.webdriver.common.by import By
     from selenium.webdriver.chrome.options import Options
     from selenium.webdriver.support.ui import WebDriverWait 
    

    【讨论】:

    • 他们有办法循环这个吗?这样它就可以测试所有元素,而不仅仅是第一个?
    • @jamjam46 更新了答案
    • data 将是一个包含值的列表
    • 这就是为什么在 while 循环中重新查找元素
    • @PDHide :对不起,我错过了那部分。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多