【问题标题】:How can I click an element in Python Selenium after finding it?找到后如何单击 Python Selenium 中的元素?
【发布时间】:2021-03-03 13:32:08
【问题描述】:

我正在创建一个脚本来获取元素的属性,如果它是真的,它会点击它。但我一直遇到错误。

我有以下代码:

from selenium import webdriver

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

choice1 = 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[1]/ng-include/label')
choice2 = 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[2]/ng-include/label')
choice3 = 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')
choice4 = 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[4]/ng-include/label')
choice5 = 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[5]/ng-include/label')

for i in choice1:
    if i.get_attribute('data-correct') == 'true':
        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[1]/ng-include/label').click()
    else:
        pass

for i in choice2:
    if i.get_attribute('data-correct') == 'true':
        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[2]/ng-include/label').click()
    else:
        pass

for i in choice3:
    if i.get_attribute('data-correct') == 'true':
        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').click()
    else:
        pass

for i in choice4:
    if i.get_attribute('data-correct') == 'true':
        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[4]/ng-include/label').click()
    else:
        pass

这是网页上的代码:

<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>

我尝试打印出选项,它们确实给了我正确的值,但是当我运行我的代码时出现此错误:

Traceback (most recent call last):
  File "Studify.py", line 106, in <module>
    '/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[1]/ng-include/label').click()
AttributeError: 'list' object has no attribute 'click'

我没有使用列表,所以我不明白为什么会出现此错误。

【问题讨论】:

  • find_elements_by_xpath 返回一个列表,find_element_by_xpath 第一个匹配,查看文档了解函数

标签: python html css python-3.x selenium


【解决方案1】:

很抱歉没有回答您最初的问题,但我建议您使用Helium。它是 Selenium 的一个很好的替代品,并且在我看来更容易使用。

【讨论】:

    【解决方案2】:

    我找到了解决方案!我使用的是driver.find_elements_by_xpath(),这是错误的。由于元素是复数并返回元素列表。我只是将driver.find_elements_by_xpath 更改为driver.find_element_by_xpath 只是删除了(s)。希望这对将来偶然发现此问题的任何人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2019-01-13
      • 1970-01-01
      • 2021-05-12
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多