Presence of text in multiple elements selenium
Question:
I want to check the presence of text prior to its retrieval in a list of elements using selenium in Python on the following link.
driver = webdriver.Firefox()
driver.get('https://www.ahs.dep.pa.gov/eFACTSWeb/searchResults_singleFacility.aspx?FacilityID=276740')
table_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '/html/body/div/form/div[3]/div[2]/table/tbody/tr/td[2]/table/tbody/tr/td')))
permit_table = [i for i in table_list if i.text == 'Facility Search Permit Details'][0]
I attempt to store in the permit_table
variable a table for which the .text
attribute contains the string "Facility Search Permit Details".
While the table_list
variable correctly retrieves the elements I am interested about (a list of all tables corresponding to the XPATH I entered), when I want to access the elements’ respective .text
attributes through list comprehension , permit_table
returns an empty list.
Is there a way to introduce an implicit wait for the visibility of the text attribute of the WebElement
corresponding to each item of the list table_list
? I am aware of the EC.text_to_be_present_in_element
method but it only works when looking for a single element, which is not my case as table_list
returns a list of elements.
Answers:
There are a few problems with your code.
-
If you were actually running your posted code, it would have thrown on this line
table_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, ...))).get_attribute("text")
with this error
AttributeError: 'list' object has no attribute 'get_attribute'
EC.visibility_of_all_elements_located()
returns a list. .get_attribute()
must be used on a single element, not a list.
-
Even if the above line was corrected, table_list
would be a collection of strings, not elements so if i.text
would not work. In your list comprehension, you are assuming i
is an element but it’s a string.
Having said all that, I’m assuming you are trying to get the TABLE that is labelled "Facility Search Permit Details" and loop through the rows, scraping the data, etc. The easier way to do this is
driver = webdriver.Firefox()
driver.get('https://www.ahs.dep.pa.gov/eFACTSWeb/searchResults_singleFacility.aspx?FacilityID=276740')
wait = WebDriverWait(driver, 20)
permit_details_table_rows = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//table[.//td[text()='Facility Search Permit Details']]/following-sibling::div[1]/table//tr")))
print(len(permit_details_table_rows))
This prints 6
.
This will give you permit_details_table_rows
which contains all rows of that TABLE that you can loop through, etc.
I want to check the presence of text prior to its retrieval in a list of elements using selenium in Python on the following link.
driver = webdriver.Firefox()
driver.get('https://www.ahs.dep.pa.gov/eFACTSWeb/searchResults_singleFacility.aspx?FacilityID=276740')
table_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '/html/body/div/form/div[3]/div[2]/table/tbody/tr/td[2]/table/tbody/tr/td')))
permit_table = [i for i in table_list if i.text == 'Facility Search Permit Details'][0]
I attempt to store in the permit_table
variable a table for which the .text
attribute contains the string "Facility Search Permit Details".
While the table_list
variable correctly retrieves the elements I am interested about (a list of all tables corresponding to the XPATH I entered), when I want to access the elements’ respective .text
attributes through list comprehension , permit_table
returns an empty list.
Is there a way to introduce an implicit wait for the visibility of the text attribute of the WebElement
corresponding to each item of the list table_list
? I am aware of the EC.text_to_be_present_in_element
method but it only works when looking for a single element, which is not my case as table_list
returns a list of elements.
There are a few problems with your code.
-
If you were actually running your posted code, it would have thrown on this line
table_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, ...))).get_attribute("text")
with this error
AttributeError: 'list' object has no attribute 'get_attribute'
EC.visibility_of_all_elements_located()
returns a list..get_attribute()
must be used on a single element, not a list. -
Even if the above line was corrected,
table_list
would be a collection of strings, not elements soif i.text
would not work. In your list comprehension, you are assumingi
is an element but it’s a string.
Having said all that, I’m assuming you are trying to get the TABLE that is labelled "Facility Search Permit Details" and loop through the rows, scraping the data, etc. The easier way to do this is
driver = webdriver.Firefox()
driver.get('https://www.ahs.dep.pa.gov/eFACTSWeb/searchResults_singleFacility.aspx?FacilityID=276740')
wait = WebDriverWait(driver, 20)
permit_details_table_rows = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//table[.//td[text()='Facility Search Permit Details']]/following-sibling::div[1]/table//tr")))
print(len(permit_details_table_rows))
This prints 6
.
This will give you permit_details_table_rows
which contains all rows of that TABLE that you can loop through, etc.