简体   繁体   English

如何在常规 chrome 而不是 chromedriver 中运行 selenium 代码?

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

相关问题 如何使用 Google Chrome 而不是 Chromedriver Python Selenium - How do I use Google Chrome instead of Chromedriver Python Selenium 如何从实际的 Chrome 安装运行 Selenium ChromeDriver 和 cookies? - how to run Selenium ChromeDriver with cookies from actual Chrome installation? 如何通过Chromedriver和Selenium启动Chrome浏览器 - How to start Chrome Browser through Chromedriver and Selenium 如何在Windows 10上安装ChromeDriver并使用Chrome运行Selenium测试? - How do I install ChromeDriver on Windows 10 and run Selenium tests with Chrome? 无法在非无头模式ChromeDriver和Chrome上运行Selenium测试 - Unable to run Selenium tests on non-headless mode ChromeDriver and Chrome 如何在多线程中运行`selenium-chromedriver` - How to run `selenium-chromedriver` in multiple threads 在Jenkins中使用chromedriver运行Selenium - Run Selenium with chromedriver in Jenkins 弃用警告:在 Windows 10 系统上通过 Selenium 使用 ChromeDriver 和 Chrome 使用选项而不是 chrome_options 错误 - DeprecationWarning: use options instead of chrome_options error using ChromeDriver and Chrome through Selenium on Windows 10 system Chrome 打开时显示“数据;” 与 selenium chromedriver - Chrome opens with “Data;” with selenium chromedriver 有没有办法将 selenium 与 chrome 而不是 chromedriver 一起使用? - Is there a way to use selenium with chrome and NOT chromedriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM