Home > Back-end >  xpath give nothing using selenium
xpath give nothing using selenium

Time:09-06

I am trying to get addresss but they give me none value can you tell me what I make mistake in xpath kindy guide us this is page link enter image description here

code:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
import pandas as pd

options = Options()
options.add_argument("--no-sandbox")
options.add_argument("start-maximized")
#options.add_experimental_option("detach", True)
webdriver_service = Service("C:\Program Files (x86)\chromedriver.exe") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service,options=options)
url = 'https://www.avocats-strasbourg.com/avocat?'
driver.get(url)
data=[]
def main():
    for x in range(44)[2:]:
        select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#edit-filter-ville'))))
        select.select_by_index(x)
        time.sleep(2)
        click_on_search_button = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '(//*[@value="Lancer la recherche"])[1]')))
        driver.execute_script("arguments[0].click();", click_on_search_button)
        
        try:
            page_links = [element.get_attribute('href') for element in 
                          WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.grid-3-small-1-tiny-1 a")))]
        except:
            pass
    
        for link in page_links:
            driver.get(link)
            time.sleep(2)
            
            try:
                address = driver.find_element(By.XPATH, "//span[contains(text(),'Adresse principale :')]/following-sibling::").text
                
            except:
                address=''
            print(address)
main()

CodePudding user response:

Try with javascript executor and use following css selector. I haven't run python scripts, however using Chome Dev tool and it is working fine.

address = driver.execute_script('return document.querySelector("h3   span.sousgras").nextSibling.textContent')

enter image description here

CodePudding user response:

Surely not the best way but anyway a way to get what you want in some way:

#creates a string with all the text inside the div
text = driver.find_element(By.XPATH, "//div[@class='ficheavocat']").text
#creates a list with the lines splitted
textlist = text.splitlines()
#in this case the info you're looking for, the address, is the 6th element
address = textlist[6]

This is the result, the address variable:

Adresse principale : 1 rue des Soeurs | 67400 ILLKIRCH-GRAFFENSTADEN

Then, of course, if you need, you can get rid of the Adresse principale : part

  • Related