All geek questions in one place

    How to wait and get the value of a Span object in Selenium Python binding

    I have code on my web page.

    <div id="" class="user_acc_setails"> <ul id="accDtlUL"> <li>First Name: <span id="f_name">Anuja</span></li> 

    By the time the page loads, the value for Sapn is not set. It takes very little time to set the value. I want to wait and get this value in my Python file.

    I am currently using the following code,

     element = context.browser.find_element_by_id('f_name') assert element.text == 'Anuja' 

    But that gives me an AssetionError . How can i solve this?

    thanks

    +4
    python unit-testing assertions selenium testcase
    AnujAroshA Sep 04 '13 at 7:36
    source share
    5 answers

    Correct, in this case it would be to use Explicit Expectation (see Python code here). So you need something like

     from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.text_to_be_present_in_element((By.Id,'f_name'), 'Anuja')) 
    +5
    Petr mensik Sep 04 '13 at 9:02
    source share

    here is the function that does this:

     def wait_on_element_text(self, by_type, element, text): WebDriverWait(self.driver, 10).until( ec.text_to_be_present_in_element( (by_type, element), text) ) 

    If the by_type parameter replaces, for example, By.XPATH, By.CSS_SELECTOR. Where the element is replaced with the element path - xpath elements, unique selector, id, etc. Where the text is replaced by the text of the element, for example. The string associated with the item.

    +3
    Ste 01 Oct '15 at 14:12
    source share
     url = "http://..." driver = webdriver.Firefox() driver.get(url) wait = WebDriverWait(driver, 10) try: present = wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, "myclassname"), "valueyouwanttomatch")) elem = driver.find_element_by_class_name("myclassname") print elem.text finally: driver.quit() 

    I realized that the wait.until returned object wait.until not an element, but a boolean, so I need to call the locate element again.

    +1
    B.Mr.W. Nov 12 '15 at 21:26
    source share

    I have found a solution. Maybe this is not the right way. What I did is used by Python sleep before finding the element and validating its value.

     import time ... time.sleep(3) element = context.browser.find_element_by_id('f_name') assert element.text == 'Anuja' 

    Then it works great.

    0
    AnujAroshA Sep 04 '13 at 7:49
    source share

    You can use the browser.implicitly_wait (2) command to wait for the browser implicitly.

     from selenium import webdriver browser = webdriver.Firefox() browser.implicitly_wait(2) 
    0
    Srinivasreddy jakkireddy Sep 04 '13 at 9:13
    source share

    Source: https://habr.com/ru/post/1500460/

    More articles:

    • Choosing rows repeating for a certain amount - sql
    • Is there any Haml port or thin one? - go
    • Mongoose ODM: how to get the first and last five elements from an array of embedded documents? - node.js
    • Uninstall the standard version of jdk 1.7 and install 1.6 on the windows. - java
    • why should fields be final in an immutable class? - java
    • The distanceFilter property of the CLLocationManager object does not work correctly - iphone
    • Java exception handling - java
    • Chrome will lose cookies - google-chrome
    • How to get all table and column names from a database in Yii Framework - php
    • Exporting static lib symbols from a DLL - c ++

    All Articles

    Geek Questions | 2019