from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
chrome_driver_path = Service(
"C:Developmentchromedriver_win32chromedriver.exe")
driver = webdriver.Chrome(service=chrome_driver_path)
URL = "https://en.wikipedia.org/wiki/Main_Page"
driver.get(URL)
articles = driver.find_element(By.XPATH, '//*[@id="articlecount"]/a[1]')
print(articles.text)
driver.quit()
WebDriver:
WebDriver object represents the browser in Selenium.
Using this object you can control the Web browser.
It is an interface of the org.openqa.selenium.* package.
Upon instantiating the implementations of this class
the browser will be launched.
FirefoxDrive, ChromeDriver,
InternetExplorerDriver, SafariDriver, OperaDriver,
HtmlUnitDriver, RemoteWebDriver are few
implementations of the WebDriver Interface.
def get_webdriver():
"""Get whatever webdriver is availiable in the system.
webdriver_manager and selenium are currently being used for this.
Supported browsers:[Firefox, Chrome, Opera, Microsoft Edge, Internet Expolorer]
Returns:
a webdriver that can be used for scraping. Returns None if we don't find a supported webdriver.
"""
try:
driver = webdriver.Firefox(
executable_path=GeckoDriverManager().install())
except Exception:
try:
driver = webdriver.Chrome(ChromeDriverManager().install())
except Exception:
try:
driver = webdriver.Ie(IEDriverManager().install())
except Exception:
try:
driver = webdriver.Opera(
executable_path=OperaDriverManager().install())
except Exception:
try:
driver = webdriver.Edge(
EdgeChromiumDriverManager().install())
except Exception:
driver = None
logging.error(
"Your browser is not supported. Must have one of the following installed to scrape: [Firefox, Chrome, Opera, Microsoft Edge, Internet Expolorer]")
return driver
var cap = ((RemoteWebDriver)driver).Capabilities;
var browserName = cap["browserName"].ToString();