How to check if some text is present on a web page using selenium 2?

后端 未结 3 1641
灰色年华
灰色年华 2021-02-07 03:10

Hi I am using selenium to automate test on web pages. I am using selenium 2 and python and would like to have answers in this framework only. SO how do I check whether some text

3条回答
  •  忘掉有多难
    2021-02-07 03:27

    You can try something like

    browser = webdriver.Firefox()
    browser.get(url)
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'some link text')))
    

    Essentially the above lines launch Firefox, navigate to the specified url, cause the browser to hold for 10 seconds, for some url to load then look for a specific link text, if no link text is found, a TimeoutException is triggered.

    Please note the number of brackets used, you will run into errors if the number of brackets does not correspond like the above.

    To be able to run the above statement, the following must have been declared

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

    This uses "element_to_be_clickable" - a full list of wait-conditions can be found here: Selenium Python: Waits

提交回复
热议问题