[英]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.我没有使用列表,所以我不明白为什么会出现此错误。
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.