Reputation: 29
I am trying to loop through different checkboxes and check them if the text is present in the list then click if not then move on.
the html element that I have been able to extract is below:
<label>
<input type ="checkbox" value = "pf29">
<span> BNN Bloomberg </span>
<span> class='count'>(16)</span>
<label>
when I do element.text it gives me BNN Bloomberg (16). Is there a way to restrict the text output to only BNN Bloomberg?
There are roughly 33 different tags so I cant use any of the string methods to limit the number of characters.
Fairly new to html and selenium so excuse my wording.
Currently this is what my code looks like
element_list = (driver.find_elements_by_css_selector("div[class='modal-dialog-padded-body'] label"))
source = element_list[3].text
print(source)
print(source) gives me BNN Bloomberg (16)
Edit: I have to loop through 21 different elements and look at the text to see what the source is and check the checkbox accordingly.
Upvotes: 0
Views: 797
Reputation: 193058
You were pretty close enough.
To retrieve the text BNN Bloomberg you need to traverse one step deeper till the first <span>
tag and youyou can use either of the following Locator Strategies:
Using css_selector
and get_attribute()
:
print(driver.find_element_by_css_selector("div.modal-dialog-padded-body label span:not(.count)").get_attribute("innerHTML"))
Using xpath
and text attribute:
print(driver.find_element_by_xpath("//div[@class='modal-dialog-padded-body']//label//span[not(contains(@class,'count'))]").text)
To print the first title you have to induce WebDriverWait for thevisibility_of_element_located()
and you can use either of the following Locator Strategies:
Using CSS_SELECTOR
and text attribute::
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.modal-dialog-padded-body label span:not(.count)"))).text)
Using XPATH
and get_attribute()
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='modal-dialog-padded-body']//label//span[not(contains(@class,'count'))]"))).get_attribute("innerHTML"))
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
You can find a couple of relevant discussions in:
Upvotes: 1
Reputation: 33384
You can try either of the following css selector.
element_list =driver.find_elements_by_css_selector("div[class='modal-dialog-padded-body'] label>span:nth-of-type(1)")
source = element_list[3].text
print(source)
OR
element_list =driver.find_elements_by_css_selector("div[class='modal-dialog-padded-body'] label>span:not(.count)")
source = element_list[3].text
print(source)
Upvotes: 1