找到元素后,如何在Python Selenium中单击该元素?

找到元素后,如何在Python Selenium中单击该元素?,python,html,css,python-3.x,selenium,Python,Html,Css,Python 3.x,Selenium,我正在创建一个脚本来获取元素的属性,如果它是真的,它会单击它。但我总是遇到一个错误 我有以下代码: 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-qui

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

我有以下代码:

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'
回溯(最近一次呼叫最后一次):
文件“Studify.py”,第106行,在
'/html/body/div[3]/div[1]/div[3]/div[1]/div/div/div[1]/study quick/div/form/div/div[2]/ng include/div[2]/div[1]/ng include/label')。单击()
AttributeError:“列表”对象没有“单击”属性

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

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

我找到了解决方案!我使用的是
驱动程序。通过xpath()查找元素是错误的。因为元素是复数的,并且返回元素列表。我只是将
driver.find_element\u by_xpath
更改为
driver.find_element\u by_xpath
刚刚删除了。希望这能帮助以后偶然发现这个问题的人。

通过xpath查找元素返回列表,通过xpath查找元素第一个匹配项,查看文档以了解函数谢谢!