[英]how to run selenium code in regular chrome instead of chromedriver?
Here is an examle code to explain my problem.这是一个示例代码来解释我的问题。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def test():
username = "gmail"
password = "password"
driver = webdriver.Chrome("C://Users//Dell//Desktop//chromedriver.exe")
driver.implicitly_wait(15)
driver.get("http://www.facebook.com")
elem = driver.find_element_by_name("email")
elem.send_keys(username)
elem = driver.find_element_by_name("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
#code after logging
test()
Whenever i run this code the code run in individual chromedriver window.每当我运行此代码时,代码都会在单独的 chromedriver window 中运行。
I want my code to do someting after logging in facebook.我希望我的代码在登录 facebook 后做一些事情。 everytime I redirect to facebook the website ask me to allow notificitions.for that reason the whole screen become black and the only clickable element is the popup.每次我重定向到 facebook 时,网站都会要求我允许通知。因此,整个屏幕变黑,唯一可点击的元素是弹出窗口。 so all the other elements are become unclickable.所以所有其他元素都变得不可点击。 so i get this error.所以我得到这个错误。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate
element:
If i could run this code in my regular browser in which i already allowed/denied notifications for facebook.如果我可以在我已经允许/拒绝 facebook 通知的常规浏览器中运行此代码。 the pop up will not raise again and the error will not happen.弹出窗口不会再次出现,错误也不会发生。
how can i run this code in my regular browser?如何在常规浏览器中运行此代码?
You can simply turn off the notifications pop up using this configuration:您可以使用此配置简单地关闭弹出通知:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# config to remove pop ups
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# you can change the values below. 0 means off and 1 means on.
opt.add_experimental_option("prefs", { \
"profile.default_content_setting_values.media_stream_mic": 1,
"profile.default_content_setting_values.media_stream_camera": 0,
"profile.default_content_setting_values.geolocation": 0,
"profile.default_content_setting_values.notifications": 1
})
def test():
username = "gmail"
password = "password"
driver = webdriver.Chrome("C://Users//Dell//Desktop//chromedriver.exe", options=opt)
driver.implicitly_wait(15)
driver.get("http://www.facebook.com")
elem = driver.find_element_by_name("email")
elem.send_keys(username)
elem = driver.find_element_by_name("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
#code after logging
test()
It's a very simple fix you can disable notifications by changing the 1 to a 0.这是一个非常简单的修复,您可以通过将 1 更改为 0 来禁用通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.