我在这里缺少什么?该脚本将像第一个配置文件一样单击,但下一个配置文件将失败。
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
driver = Chrome()
driver.get('https://www.okcupid.com/login')
redirect = ('https://www.okcupid.com/doubletake')
username = driver.find_element_by_name('username')
password = driver.find_element_by_name("password")
username.send_keys("gmail.com")
password.send_keys("password")
#driver.find_element_by_xpath("//input[@class='login2017-actions-button']").click()
driver.find_element_by_class_name('login2017-actions-button').click()
time.sleep(5)
driver.get(redirect)
hoes = driver.find_element_by_xpath("//button[@class='cardactions-action cardactions-action--like']")
while 1:
print('Liking')
hoes.click()
time.sleep(5)
但是当我进行这些更改时,它根本不起作用:
hoes = driver.find_elements_by_xpath("//button[@class='cardactions-action cardactions-action--like']")
for matches in hoes:
print('Liking')
hoes.click()
time.sleep(5)
Liking
Traceback (most recent call last):
File "/home/cha0zz/Desktop/scripts/okcupid.py", line 24, in <module>
hoes.click()
AttributeError: 'list' object has no attribute 'click'
最佳答案
这里
hoes = driver.find_element_by_xpath("//button[@class='cardactions-action cardactions-action--like']")
while 1:
print('Liking')
hoes.click()
find_element_by_xpath
返回单个对象。这意味着您可以为其调用 click
(当然,如果找到该元素),但您无法迭代 - 单个元素不是列表,并且没有定义的 __iter__
成员.
在您的其他示例代码中
hoes = driver.find_elements_by_xpath("//button[@class='cardactions-action cardactions-action--like']")
for matches in hoes:
print('Liking')
hoes.click()
time.sleep(5)
hoes
现在是一个列表,因此您无法单击它,但可以迭代。
也许您想点击 hoes
的每个成员,您可以通过一个小修复 hoes.click()
=> match.click()
:
hoes = driver.find_elements_by_xpath("//button[@class='cardactions-action cardactions-action--like']")
for matches in hoes:
print('Liking')
matches.click()
time.sleep(5)
关于python - 类型错误 : 'WebElement' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50753754/