【发布时间】:2021-06-01 18:58:32
【问题描述】:
我有一个包含以下代码的网页:
<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>
我正在创建一个检查data-correct 标签的脚本。如果它是真的,那么它应该点击它。它们都具有相同的 xpath 和标签。那么我该怎么做呢?
他们都有这个 xpath:
/html/body/div[3]/div[1]/div[3]/div[1]/div/div[1]/study-quiz/div/form/div/div[2]/ng
include/div[2]/div[3]/ng-include/label```
这是我目前的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
elems = driver.find_elements_by_xpath(
'/html/body/div[3]/div[1]/div[3]/div[1]/div/div[1]/study-quiz/div/form/div/div[2]/ng-include/div[2]/div[3]/ng-include/label')
count = 0
while(count != len(elems)):
elems = WebDriverWait(driver, 15).until(
EC.presence_of_all_elements_located(By.XPATH, '/html/body/div[3]/div[1]/div[3]/div[1]/div/div[1]/study-quiz/div/form/div/div[2]/ng-include/div[2]/div[3]/ng-include/label'))
elems[count].click()
count += 1
print(f'found {count} answer')
【问题讨论】:
标签: python html css python-3.x selenium