Home > Software design >  xpath issue using selenium
xpath issue using selenium

Time:03-07

I am trying to scrape title but they say your xpath is wrong

from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep

PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.nationalhardwareshow.com/en-us/attend/exhibitor-list.html'
driver =webdriver.Chrome(PATH)
driver.get(url)
sleep(2)
def searchplace():
    vid = driver.find_elements(By.XPATH, "//div[@class='row']")
    for item in vid:
        title=item.find_element_by_xpath(".//div[@class='company-info']//h3").text
        print(title)
searchplace()

CodePudding user response:

Don't know what to say, web scraping is always susceptible to suddenly not work. You might need to check that your XPATH actually points to the correct element.

CodePudding user response:

The below xpath worked for me give a try. Also try to reduce the xpath length efficiently if you need.

vid = driver.find_elements(By.XPATH, "//div[@class='directory-item directory-item-feature-toggled exhibitor-category']")
for item in vid:
    title=item.find_element(By.XPATH, "div[@class='row']/div[2]/div//div[@class='company-info']/div/a/h3")
    print(title.text)

CodePudding user response:

You are using a wrong locators here.
Each vid block with this XPath: //div[contains(@class,'directory-item directory-item-feature-toggled')].
With that your code will look like:

from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep

PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.nationalhardwareshow.com/en-us/attend/exhibitor-list.html'
driver =webdriver.Chrome(PATH)
driver.get(url)
sleep(2)
def searchplace():
    vid = driver.find_elements(By.XPATH, "//div[contains(@class,'directory-item directory-item-feature-toggled')]")
    for item in vid:
        title=item.find_element_by_xpath(".//div[@class='company-info']//h3").text
        print(title)
searchplace()

I would advice you using Expected Conditions explicit waits instead of hardcoded pauses.
With it your code will be:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep

PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.nationalhardwareshow.com/en-us/attend/exhibitor-list.html'
driver =webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 20)

driver.get(url)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'directory-item directory-item-feature-toggled')]")))

sleep(0.3) #leaved short delay to make sure not only the first item got visible
def searchplace():
    vid = driver.find_elements(By.XPATH, "//div[contains(@class,'directory-item directory-item-feature-toggled')]")
    for item in vid:
        title=item.find_element_by_xpath(".//div[@class='company-info']//h3").text
        print(title)
searchplace()
  • Related