简体   繁体   中英

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.

I want my code to do someting after logging in 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM