I am using selenium to print all of the product links on a webpage. Here is my code:
from selenium.webdriver import Chrome
driver_path = PATH = r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver = Chrome(executable_path=driver_path)
driver.get('https://www.lazada.com.ph/shop-portable-speakers/?spm=a2o4l.home.cate_2_2.2.239e359dxynAFV'
)
xpath = '//*[@id="root"]/div/div[2]/div[1]/div/div[1]/div[2]'
link_elements = driver.find_elements_by_xpath(xpath)
links = []
for link_el in link_elements:
href = link_el.get_attribute("href")
print (href)
links.append(href)
driver.quit()
The code runs and prints the following:
runfile('C:/Users/David/Desktop/Selenium/untitled1.py', wdir='C:/Users/David/Desktop/Selenium')
C:\Users\David\Desktop\Selenium\untitled1.py:4: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = Chrome(executable_path=driver_path)
C:\Users\David\Desktop\Selenium\untitled1.py:10: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
link_elements = driver.find_elements_by_xpath(xpath)
None
Any ideas as to what I am doing wrong here?.
Solution
With selenium4 as the key executable_pat
h is deprecated you have to use an instance of the Service()
class along with ChromeDriverManager().install()
command as discussed below.
Pre-requisites Ensure that:
Selenium is upgraded to v4.0.0
pip3 install -U selenium
Webdriver Manager for Python is installed
pip3 install webdriver-manager
Selenium v4 compatible Code Block
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
Also use
driver.find_elements(By.XPATH, "xpath")
instead of
driver.find_elements_by_xpath(xpath)