Python 3.x Selenium.click()不工作。接收错误';列表';对象没有属性';单击';
.click()工作不正常,返回错误:“list”对象没有属性“click”。任何帮助都将不胜感激Python 3.x Selenium.click()不工作。接收错误';列表';对象没有属性';单击';,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,.click()工作不正常,返回错误:“list”对象没有属性“click”。任何帮助都将不胜感激 from selenium.webdriver.common.keys import Keys from time import sleep, strftime from random import randint import pandas as pd chromedriver_path = 'C:/Users/Enrico/Downloads/chromedriver_win32/chrom
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
import pandas as pd
chromedriver_path = 'C:/Users/Enrico/Downloads/chromedriver_win32/chromedriver.exe'
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)
username = webdriver.find_element_by_name('username')
username.send_keys('username')
password = webdriver.find_element_by_name('password')
password.send_keys('password')
password.send_keys(Keys.ENTER)
sleep(3)
notnow = webdriver.find_elements_by_css_selector('body > div:nth-child(13) > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
notnow.click()
这是不正确的。你不应该使用
notnow = webdriver.find_elements_by_css_selector('body > div:nth-child(13) > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
您应该使用按css选择器查找元素,而不是按css选择器查找元素 除非您希望notnow是返回的元素列表,否则应该执行循环并单击正确的元素。像这样的
.find_elements*
返回元素列表,所以您需要在调用之前放置索引。单击()
方法
notnow = webdriver.find_elements_by_css_selector('body > div:nth-child(13) > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
print(len(notnow))
if len(notnow) > 0:
notnow[0].click()
else:
print('element not found')
以上假设您需要单击第一个元素。我正在尝试通过包含两个不同元素的通知弹出窗口。现在还没有。我使用css作为按钮,并尝试使用.click()来选择not now按钮,但是.click()根本不起作用,即使是在单个元素上。您好,当我尝试时,我得到了错误:列表索引超出范围。@Enricoshipple Try
print(len(notnow))
,如果结果是0
,似乎您使用了错误的定位器或元素,您的意思是不显示。请再次尝试并向我提供信息。@Enricoshipple我已经更新了答案,如果有条件,请查看。@Enricoshipple或者您可以提供HTMLnotnow
?也许我们可以换一下定位器。嗨,我刚从急诊室出来。我现在好多了。我会检查代码,让你知道我收到了什么。我感谢你的帮助@弗里安人
notnow = webdriver.find_elements_by_css_selector('body > div:nth-child(13) > div > div > div > div.mt3GC > button.aOOlW.HoLwm')
print(len(notnow))
if len(notnow) > 0:
notnow[0].click()
else:
print('element not found')