[英]How to close google chrome popups with selenium on python?
I am trying to automate sending messages on facebook messanger using python. I have this code until now:我正在尝试使用 python 在 facebook messanger 上自动发送消息。我现在有这段代码:
from selenium import webdriver
class Bot: class 机器人:
def __init__(self):
self.driver = webdriver.Chrome()
def loginFacebook(self):
# btn = self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div/div[1]/form/div[1]/div[1]/input")
#btn.click()
email_in = self.driver.find_element_by_xpath('//*[@id="email"]')
email_in.send_keys("xxxxx@mail.com")
pw_in = self.driver.find_element_by_xpath('//*[@id="pass"]')
pw_in.send_keys("xxxxxx")
login_btn = self.driver.find_element_by_xpath('//*[@id="u_0_d"]')
login_btn.click()
def sendMessageFacebook(self):
sendMessageBtn = self.driver.find_element_by_xpath('//*[@id="mount_0_0"]/div/div[1]/div[1]/div[5]/div[1]/div[2]/span/div')
sendMessageBtn.click()
The problem is the facebook page after the login opens a pop up to "Allow notifications" which I have to click to Allow or to Block.问题是登录后的 facebook 页面打开一个弹出窗口“允许通知”,我必须单击以允许或阻止。 And this pop up doesn't allow the driver to find the element where the send message button is... But if you click several times on the page (like 4 or 5 times on the page the blur disappears and allows to click the button manually. How can I click several times on a page or close the popup to be able to find the element?并且此弹出窗口不允许驱动程序找到发送消息按钮所在的元素...但是如果您在页面上单击多次(例如在页面上单击 4 或 5 次,模糊消失并允许单击按钮手动。如何在页面上单击几次或关闭弹出窗口才能找到元素?
from selenium.webdriver.chrome.options import Options
option = Options()
option.add_argument("--disable-infobars")
driver = webdriver.Chrome("yourpathtochromedriver",chrome_options=option)
this will disable all infobars.这将禁用所有信息栏。
The popup on a website is an other frame of the webpage.网站上的弹出窗口是网页的另一个框架。 So you have to switch to an other frame in order to close it.所以你必须切换到另一个框架才能关闭它。 By using:通过使用:
driver.switch_to_active_element()
You ll be able to go to the frame popup.您将能够 go 到框架弹出窗口。 If it doesn't work you can try the other functions that exist to change to an other frame.如果它不起作用,您可以尝试现有的其他功能以更改为其他框架。 driver.switch_to_bla_bla_bla() driver.switch_to_bla_bla_bla()
Then, you ll be able to scrape the popup like the webpage and you ll be able to click on the "close" button or something like that: Hope I helped you :)然后,您将能够像网页一样抓取弹出窗口,并且您将能够单击“关闭”按钮或类似的按钮:希望我对您有所帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.