简体   繁体   English

如何在 python 上使用 selenium 关闭 google chrome 弹出窗口?

[英]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.

相关问题 处理 selenium python 中的弹出窗口 - handle popups in selenium python 如何使用python硒关闭另一个Chrome弹出窗口 - How to close yet another chrome popup using python selenium 如何使用Selenium,Chrome驱动程序和Python关闭新建的标签页 - How to close newly constructed tab using selenium, chrome driver and python 如何使用 selenium webdriver 和 python 关闭 chrome 浏览器弹出对话框 - how to close chrome browser popup dialog using selenium webdriver and python Python 如何在关闭后杀死所有 selenium/chrome 进程? - Python how to kill all selenium/ chrome processes after close? 如何在python中将现有的Google Chrome配置文件与Selenium Chrome Webdriver一起使用? - How to use an existing google chrome profile with selenium chrome webdriver in python? 如何通过弹出窗口并继续使用 Selenium 和 Python 登录 - How to get past popups and continue to login using Selenium and Python 如何自动在Google Chrome浏览器中打开新标签页? 硒Python - How to automate opening of new tabs on Google Chrome? Selenium Python 如何使用 Google Chrome 而不是 Chromedriver Python Selenium - How do I use Google Chrome instead of Chromedriver Python Selenium 如何使用 python selenium 访问 google chrome 开发人员工具上的安全面板? - How to access Security panel on google chrome developer tools with python selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM