Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

getting text value from xpath

$
0
0

why do i need to use this code to get the text value from the x path

from selenium import webdriverfrom selenium.webdriver.common.by import Byimport timewith open('restaurants.csv', 'w', encoding='utf-8') as file:    file.write('name \n')driver = webdriver.Chrome()driver.get('https://www.tripadvisor.co.nz/Restaurants-g1811027-Auckland_North_Island.html')time.sleep(3)names = driver.find_elements(By.XPATH, "//div[@class='biGQs _P fiohW alXOW NwcxK GzNcM ytVPx UTQMg RnEEZ ngXxk']/a")with open('restaurants.csv', 'a', encoding='utf-8') as file:    for name in names:        file.write(name.text +'\n')file.closedriver.quit()

but when i used my original code it would not allow me to get the text value from the xpath why?

from selenium import webdriverfrom selenium.webdriver.common.by import Byimport timewith open('restaurants.csv', 'w', encoding='utf-8') as file:    file.write('name \n')driver = webdriver.Chrome()driver.get('https://www.tripadvisor.co.nz/Restaurants-g1811027-Auckland_North_Island.html')time.sleep(3)name  = driver.find_elements(By.XPATH,"//div[@class='biGQs _P fiohW alXOW NwcxK GzNcM ytVPx UTQMg RnEEZ ngXxk']/a")with open('restaurants.csv', 'a', encoding='utf-8') as file:    for i in range(len(name)):        file.write(name.text +'\n')file.closedriver.quit 

thanks :)


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>