Getting Specific Elements In Selenium
Solution 1:
Seems you were pretty close enough.
[<selenium.webdriver.remote.webelement.WebElement (session="1c044455cf883fdedf6845bcd456bfab", element="0.23338884730774767-2")>]
represents the element where as you were looking for the text within the element.
To extract the texts e.g. N06D-X, N07X, etc from all of the <p>
tags using Selenium and python you have to induce WebDriverWait for visibility_of_all_elements_located()
and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
andget_attribute("innerHTML")
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "li.data-list__property#who-atc-codes span.data-list__property-value p")))])
Using
XPATH
and text attribute:print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//li[@class='data-list__property' and @id='who-atc-codes']//span[@class='data-list__property-value']//p")))])
Note : You have to add the following imports :
from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC
Outro
Link to useful documentation:
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.
- Difference between text and innerHTML using Selenium
Solution 2:
you dont search in whole website but in previously found object
li_object = driver.find_elements_by_id('who-atc-codes')
lst = li_object.find_element_by_tag_name("span").find_elements_by_tag_name("p")
for p in lst:
print(p.text)
print(p.get_attribute('innerHTML'))
or you can try
span_object = li_object.find_element_by_tag_name("span")
print(span_object.get_attribute('innerHTML'))
Solution 3:
Use following css selector
to get list of items and then iterate.
To get the text You can use either .text
or .get_attribute("innterHTML")
or .get_attribute("textContent")
Code:
items=driver.find_elements_by_css_selector("span.data-list__property-value>p")
for item in items:
print(item.text)
print(item.get_attribute("innterHTML"))
print(item.get_attribute("textContent"))
#To get only value from string use spilt and take the first element.print(item.text.strip().split(" ")[0])
print(item.get_attribute("innterHTML").strip().split(" ")[0])
print(item.get_attribute("textContent").strip().split(" ")[0])
Post a Comment for "Getting Specific Elements In Selenium"