python - 类型错误 : 'WebElement' object is not iterable

标签 python selenium

我在这里缺少什么?该脚本将像第一个配置文件一样单击,但下一个配置文件将失败。

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/

相关文章:

python - WebDriverException : Message: unknown error: Chrome failed to start: crashed error using ChromeDriver Chrome through Selenium Python on Amazon Linux

firefox - 使用 Selenium WebDriver 的 FireFox 中的下拉行为问题

Python正则表达式通过AND条件查找匹配词

python - numpy 中 ogrid 的 dtype 是否可以指定?

python - 类型错误 : Object of type 'bytes' is not JSON serializable

apache - 无法解析 Log4j-core 2.3 上的 BasicConfigurator 类

java - "No Alert Open"在网站完全打开之前尝试输入登录凭据时

scala - uber jar 中的 NoSuchMethodError 异常

python - ElasticBeanstalk Django : Please supply ENGINE value

python - Pandas GroupBy 日期范围取决于每一行