[英]How can I click multiple items in Python Selenium with the same xpath?
我有一個包含以下代碼的網頁:
<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
label。 如果它是真的,那么它應該點擊它。 它們都具有相同的 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')
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver_path = '' # Chromedriver executable Path
driver = webdriver.Chrome(executable_path=r'/home/severus/Desktop/drivers/chromedriver')
elems = driver.find_elements(By.XPATH, "//label[@data-correct='true']") # Get all elemts where Attr data-correct='true'
for elem in elems:
# clicks all data-correct='true' Labels.
elem.click()
您可以使用“driver.find_elements_by_xpath()”function,它將返回元素列表(如果存在),然后應用循環並執行您的邏輯。
使用 css 選擇器更容易,
# select <label> that has attribute data-correct
elems = driver.find_elements_by_css_selector('label[data-correct]')
for elem in elems:
if elem.get_attribute('data-correct') == "true":
elem.click()
else:
print("false")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.