【问题标题】:How can I click multiple items in Python Selenium with the same xpath?如何使用相同的 xpath 在 Python Selenium 中单击多个项目?
【发布时间】: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


    【解决方案1】:

    您可以使用“driver.find_elements_by_xpath()”函数,它将返回元素列表(如果存在),然后应用循环并执行您的逻辑。

    【讨论】:

    • 但是我怎么知道是真是假呢?
    • element.is_selected()
    【解决方案2】:
    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()
    
    
    
    

    【讨论】:

    • 更新问题
    【解决方案3】:

    使用 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")
    

    【讨论】:

    • 返回AttributeError: 'list' object has no attribute 'click'
    • elem 不是list 你写对了吗,而不是elemss
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2020-04-12
    • 2012-12-21
    • 1970-01-01
    • 2021-09-08
    • 2017-05-29
    相关资源
    最近更新 更多