0

I am try to scrape data but they will provide me an error this is website link https://www.dastelefonbuch.de/Suche/Zahnarzt

from selenium import webdriver
PATH="C:\Program Files (x86)\chromedriver.exe"
url='https://www.dastelefonbuch.de/Suche/Zahnarzt'
driver =webdriver.Chrome(PATH)
driver.get(url)
vid=driver.find_element_by_xpath("//div[@class='vcard']")
for item in vid:
 title=item.find_element_by_xpath("//div[@class='name']").text
 phone=item.find_element_by_xpath("//span[@class='nr']").text
 website=item.find_element_by_xpath("//i[@class='icon icon_url']").text
 print(title,phone,website)
 
asked Nov 18, 2021 at 11:55

2 Answers 2

1

Instead of vid=driver.find_element_by_xpath("//div[@class='vcard']") which returns a WebElement you need vid=driver.find_element_by_xpath("//div[@class='vcard']") which would return a list to iterate as follows:

from selenium.webdriver.common.by import By
vid = driver.find_elements(By.XPATH, "//div[@class='vcard']")
for item in vid:
 title=item.find_element_by_xpath("//div[@class='name']").text
 phone=item.find_element_by_xpath("//span[@class='nr']").text
 website=item.find_element_by_xpath("//i[@class='icon icon_url']").text
 print(title,phone,website)
answered Nov 18, 2021 at 11:57

5 Comments

they give these error NameError: name 'find_elements_by_xpath' is not defined
Checkout the answer update and let me know the status
they give us wong output repeatedly print one name many time
and also not provide website link
@AmenAziz This answer was based on find_element_by_xpath() and find_elements_by_xpath() stuff, which I strongly believe have got you started in the right direction. repeatedly print one name sounds like a different issue all together and you may like to raise a new question.
0

When you extract xpath on an item you have to change a bit xpath, saying lookup on current node with ".//"

Change

title=item.find_element_by_xpath("//div[@class='name']").text

to

title=item.find_element_by_xpath(".//div[@class='name']").text

Checkout and feedback if it works

answered Nov 18, 2021 at 12:37

2 Comments

it give me the correct but why put dot can you tell me
is cause // means global (all html), and .// means from the node (web element, your div). Also just one / means direct childrens. Here you can get more info about it and more complexity Xpaths en.wikipedia.org/wiki/XPath

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.