如何在python中返回标记内的项

如何在python中返回标记内的项,python,html,python-3.x,selenium,Python,Html,Python 3.x,Selenium,我在网页上有以下代码: <label data-correct="false"> <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion" ng-value="option" name="q0" class="ng-pristine ng-untouched ng

我在网页上有以下代码:

<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>
我对python和selenium非常陌生,所以我不确定下一步该怎么做

data = [ i.get_attribute("data-correct") for i in driver.find_elements_by_tag_name('label')]
data correct是label元素的一个属性,因此如果能够正确定位元素,则可以使用get_属性

如果您不想获取标记,而是想单击它,那么可以使用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 wait:

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

这是一种循环的方式吗?这样它就可以测试所有的元素,而不仅仅是第一个元素了?@jam46更新后的应答器数据将是一个包含值的列表,这就是为什么在while中重新填充元素的原因loop@PDHide对不起,我错过了那部分+1.
 from selenium.webdriver.common.by import By
 from selenium.webdriver.chrome.options import Options
 from selenium.webdriver.support.ui import WebDriverWait