Reputation: 4062
I am using Selenium Webdriver with Python.
I have a search results page which displays a list of products.
I want to loop through the list and print out the text value of the h2 tag.
E.g. it should print out F1 2015 (PS4)
I know i should use self.driver_find_elements to get all the elements into a list.
I should then use a for loop to iterate through and print out it's value.
My code is not working. It prints out a list of the representation of the object. It does not print out F1 2015 (PS4)
This is my selenium python code:
def click_search(self):
search_field = self.driver.find_element(By.XPATH, '//*[@id="twotabsearchtextbox"]')
search_field.send_keys("F1")
search_button = self.driver.find_element(By.XPATH, './/*[@id="nav-search"]/form/div[2]/div/input').click()
searchresults_text = self.driver.find_elements(By.XPATH, '//div[@class="a-row a-spacing-small"]//a/h2')
print searchresults_text
for i in searchresults_text:
print searchresults_text
The html snippet is:
<div class="a-row a-spacing-small">
<a class="a-link-normal s-access-detail-page a-text-normal" href="http://localhost:8080/testurl/i=1437258749&sr=8-1&keywords=f1" title="F1 2015 (PS4)">
<h2 class="a-size-medium a-color-null s-inline s-access-title a-text-normal">F1 2015 (PS4)</h2>
</a>
The output to the console is:
<selenium.webdriver.remote.webelement.WebElement object at 0x0282F290>,
<selenium.webdriver.remote.webelement.WebElement object at 0x0282F2B0>,
<selenium.webdriver.remote.webelement.WebElement object at 0x0282F2D0>]
Process finished with exit code 0
Upvotes: 2
Views: 11413
Reputation: 330063
First elements of searchresults_text
are bound to i
but you never use it and try to print searchresults_text
instead. Moreover to get text content you can use text
property:
for el in searchresults_text:
print el.text
Upvotes: 4