我正在从Youtube视频中"复制"代码(https://github.com/aj-4/ig-followers/blob/master/main.py(以更熟悉Web驱动程序。
我正在运行Linux Mint和Chromium 85。
它应该用我的用户数据登录instagram,并关闭出现"现在不"的两个窗口。登录非常有效,在此过程中单击"提交"按钮也是如此,但我的代码无法找到必须单击才能继续的两个"现在不"按钮。我试图通过检查Chromium中的按钮来使用我复制的完整XPath指定按钮(就像我对提交按钮所做的那样(。此外,将按钮指定为("//button[contains(text(), 'Not Now')]")
也不起作用。我还尝试使用buttons类(如图所示https://devhints.io/xpath(。
from selenium import webdriver
from time import sleep
from logindata import un , pw
class instabot:
def __init__(self, un, pw):
link = ('https://www.instagram.com')
self.driver = webdriver.Chrome()
self.driver.get(link)
sleep(1)
self.driver.find_element_by_xpath("//input[@name="username"]")
.send_keys(un)
self.driver.find_element_by_xpath("//input[@name="password"]")
.send_keys(pw)
sleep(1)
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button/div")
.click()
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")
.click()
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")
.click()
sleep(2)
print('FINISH TESTING? [y/n]')
input() == (X)
if X == 'y':
self.driver.close()
else:
sleep(30)
instabot(un, pw)
运气也不好。我每次都会犯同样的错误。
Traceback (most recent call last):
File "instabot.py", line 41, in <module>
instabot(un, pw)
File "instabot.py", line 24, in __init__
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")
File "/home/anton/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/anton/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/anton/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/anton/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(text(), 'Not Now')]"}
(Session info: chrome=85.0.4183.83)
我对编码还相当陌生,但即使我也知道如何咨询谷歌。这对他没有帮助,所以我求助于你们。我们真诚地感谢您的每一个回答。
提前谢谢。
Hi‘Not Now’未被点击,因为这些是弹出警报,硒无法识别并点击‘Not Now">
尝试使用以下命令,
driver.switch_to_alert((.sdisse((#这将使用"现在不"选项来解除