我在这里缺少什么?该脚本将在第一个配置文件中单击,但在下一个配置文件中失败。
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'
答案 0 :(得分:2)
下面
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()
=&gt; 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)