简体   繁体   English

找到后如何单击 Python Selenium 中的元素?

[英]How can I click an element in Python Selenium after finding it?

I am creating a script that gets the attribute of an element and if it's true it clicks it.我正在创建一个获取元素属性的脚本,如果它是真的,它会单击它。 But I keep running into an error.但我一直遇到错误。

I have the following code:我有以下代码:

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

This is the code on the webpage:这是网页上的代码:

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

I tried printing out the choices and they did give me the correct values but I get this error when I run my code:我尝试打印出选择,它们确实给了我正确的值,但是当我运行代码时出现此错误:

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'

I'm not using a list so I don't understand why I'm getting this error.我没有使用列表,所以我不明白为什么会出现此错误。

Sorry for not answering your original question but I would recommend using Helium .很抱歉没有回答您最初的问题,但我建议您使用Helium It is a good alternative to Selenium and is much easier to use in my opinion.它是 Selenium 的一个很好的替代品,并且在我看来更容易使用。

I found the solution!我找到了解决方案! I was using driver.find_elements_by_xpath() which is wrong.我使用的是错误的driver.find_elements_by_xpath() Since elements is plural and returns a list of elements.由于元素是复数并返回元素列表。 I simply changed driver.find_elements_by_xpath to driver.find_element_by_xpath just removed the (s).我只是将driver.find_elements_by_xpath更改为driver.find_element_by_xpath刚刚删除了(s)。 Hope this helps anyone who stumbles upon this issue in the future.希望这对将来偶然发现此问题的任何人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Python 和 Selenium 找到它后,我无法单击该元素 - I can not click on this element after finding it using Python and Selenium 成功找到元素后无法在Python Selenium中单击元素 - Can't Click an Element in Python Selenium After Successfully Finding It 我无法单击按钮并且硒找不到元素硒python - I cant click in a button and selenium not finding element selenium python 如何使用硒和python单击网页上的元素 - How can i click the element on webpage using selenium with python 我如何单击 selenium python 中列表的特定类别元素 - How can i click specific category element of list in selenium python 如何在 python 中使用 selenium 在不和谐页面上单击此元素? - how can i click this element on the discord page with selenium in python? 使用 Selenium 和 Python 在 html 中查找要单击的元素 - Finding element to click in html with Selenium and Python Python Selenium PhantomJS-禁用Javascript后,我无法单击任何内容或获取任何元素 - Python Selenium PhantomJS - After disabling Javascript I can't click on anything or get any element 使用 Selenium+Python,我正在使用 find element 命令查找一个元素,但该元素是动态的,如何防止它抛出异常? - Using Selenium+Python, I am finding an element with the find element command, but the element is dynamic, how can I prevent it throwing an exception? 如何在不使用元素的情况下使用Python在Selenium上按Enter键? - How can I press the Enter key on Selenium using Python without finding an element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM