简体   繁体   中英

Proper syntax for find_element in Selenium Python

I am attempting to build a web scraper in Selenium. The first thing I want the script to do is to log in with my credentials into a database. Thus, I attempted to use find_element in order locate the user id/password entry fields.

However, when I used find_element, I was thrown the following error: "By" is not defined Pylance (reportUndefinedVariable).

I've read the Selenium 4 documentation at selenium.dev, but I'm not sure what exactly has gone wrong here.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.ie.service import Service as IEService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.microsoft import IEDriverManager


def test_driver_manager_chrome():
    service = ChromeService(executable_path=ChromeDriverManager().install())

    driver = webdriver.Chrome(service=service)

    driver.quit()

#open the browser

driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.maximize_window

#navigate to the website

driver.get("URL")

#log in 

userID = driver.find_element(By.ID, "_58_login")

I've found the solution - By needs to be imported separately. Thus a separate line must be added to this code:

from selenium.webdriver.common.by import by 

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