我如何在Python硒找到它后单击元素?



我正在创建一个脚本,获得一个元素的属性,如果它是真的,它点击它。但是我总是出错。

我有以下代码:

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'

我没有使用列表,所以我不明白为什么我得到这个错误。

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

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

最新更新