This is an example of XPATH to count the elements
$x("count(//div[contains(text(),'domain')])")
◄ you can test this in the browser
The below code should work if the wait condition is satisfied (find a "p" tag where the text contains "domain".
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.FirefoxOptions()
options.add_argument("--headless") # Ensure GUI is off
webdriver_service = Service("/home/myuser/geckodriver/geckodriver")
browser = webdriver.Firefox(service=webdriver_service, options=options)
# Get element with tag name 'div'
xpath_count_condition="bolean(count(//p[contains(text(), 'domain')])==1)"
browser.get("https://www.example.com")
wait=WebDriverWait(browser, 3).until(lambda browser:
browser.find_element(by=By.XPATH,value=xpath_count_condition))
element = browser.find_element(By.TAG_NAME, 'lulu')
# Get all the elements available with tag name 'p'
elements = element.find_elements(By.TAG_NAME, 'p')
for e in elements:
print(e.text)
I am getting this error
InvalidSelectorException: Message: Given xpath expression "bolean(count(//p[contains(text(), 'domain')])==1)" is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression
I would expect that once the above works I can turn that into a negative search (count==0) hence the subject line which is what I am after
CodePudding user response:
To wait for all the <div>
elements with an ID not to contain a certain string you need to induce WebDriverWait for the invisibility_of_element_located() and you can use either of the following locator strategies:
Using xpath:
WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.XPATH, "//div[contains(@id, 'domain')]")))
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
References
You can find a relevant detailed discussion in: