Issue
I have the following sample HTML:
<div class="person">
<div class="title">
<a href="http://www.url.com/name/">John Smith</a>
</div>
<div class="company">
<a href="http://www.url.com/company/">SalesForce</a>
</div>
</div>
<div class="person">
<div class="title">
<a href="http://www.url.com/name/">Phil Collins</a>
</div>
<div class="company">
<a href="http://www.url.com/company/">TaskForce</a>
</div>
</div>
<div class="person">
<div class="title">
<a href="http://www.url.com/name/">Tracy Beaker</a>
</div>
<div class="company">
<a href="http://www.url.com/company/">Accounting</a>
</div>
</div>
I am trying to iterate through the list to try and get the following results:
John Smith, SalesForce
Phil Collins, TaskForce
Trace Beaker, Accounting
I am using the following code:
persons = []
for person in driver.find_elements_by_class_name('person'):
title = person.find_element_by_xpath('.//div[@class="title"]/a').text
company = person.find_element_by_xpath('.//div[@class="company"]/a').text
persons.append({'title': title, 'company': company})
However, the above code only iterates through the first person and not through all the people. Any help is appreciated.
Solution
As you are able to iterate through the first person details that implies your logic is perfect but to consider all the persons you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategy:
persons = []
for person in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASSNAME, "person")))
title = person.find_element_by_xpath('.//div[@class="title"]/a').text
company = person.find_element_by_xpath('.//div[@class="company"]/a').text
persons.append({'title': title, 'company': company})
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.