0

Hi I am using selenium to automate test on web pages. I am using selenium and python and would like to have answers in this framework only. I run looping script to check if text is still found or not, if not then close the browser.I have tried my script but only working when text has link on it

    while True:
        try:
            element = WebDriverWait(driver, 5).until(
           EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "My Text"))  
            )
    
        except:
            break
    driver.close()
    driver.quit()

that script is working when text has link, The problem is my text was pure text without any link. i cannot use css selector because the text changing after certain minutes, so i need to locate text not xpath or other. Hope someone can help. thank you

2 Answers2

0

You are using By.PARTIAL_LINK_TEXT which is designed to only find text inside links. If you want to lookup text in all elements, you need to use XPATH:

EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'My Text')]")) 

See for reference: https://stackoverflow.com/a/18701085/14241710

Muhammad Faiq
  • 299
  • 2
  • 6
0

try that out, might help:

def check_no_longer_present(text):
   try:
       if driver.find_element_by_xpath("//[contains(text(), '{}')".format(text)).is_displayed():
           return False
   except NoSuchElementException:
       return True

if check_no_longer_present('you_text'):
    driver.quit()
Vova
  • 3,117
  • 2
  • 15
  • 23