简体   繁体   中英

How can i print the value in Selenium, i am only getting <generator object <genexpr> at, and find_elements does not work

im am trying to get the values i added to a list with selenium and print them out. But i am only getting this: <generator object at 0x000001B924EC7990>. How can i print the values in the list.

I also tried to shorten the xpath with "//tr[@class= 'text3'][11]/td" but it didnt work.

Like you can see i tried to loop through the list and convert it in text, but it also didnt work.

Would this work range(driver.find_elements(By.XPATH,"//table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[3]/table/tbody/tr[2]/td[position() >= last()]"))?

Can you guys help me out?

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

driver = webdriver.Chrome()
driver.implicitly_wait(10)
website = "https://langerball.de/"
driver.get(website)

for i in range(7):
    xpath_test = "//table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[3]/table/tbody/tr[2]/td[position() >= last()]"
    a = driver.find_elements(By.XPATH, xpath_test)
    test_li = []
    test_li.append(a)
print(b.text for b in test_li)

driver.find_elements method returns a list of web elements while you are looking for their text values. Web element text value can be received by applying the .text method on a web element.
So, you should iterate over the received list of web elements and extract text from each web element in the list.
Also test_li = [] should be defined out of the loop.
So your code could be something like this:

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

driver = webdriver.Chrome()
driver.implicitly_wait(10)
website = "https://langerball.de/"
driver.get(website)
test_li = []
for i in range(7):
    xpath_test = "//table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[3]/table/tbody/tr[2]/td[position() >= last()]"
    a_list = driver.find_elements(By.XPATH, xpath_test)   
    for a in a_list:
        test_li.append(a.text)
print(b.text for b in test_li)

PS
I'm not sure about the rest of your code: the for i in range(7) loop and the xpath_test XPath expression

This worked

test_li = []

xpath_test = "//table[2]/tbody/tr/td[2]/table[1]/tbody/tr/td[3]/table/tbody/tr[2]/td[position() <= last()]"
a_list = driver.find_elements(By.XPATH, xpath_test)   
for a in a_list:
    test_li.append(a.text)
print(test_li)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM