Home > Mobile >  My WebDriverWait is returning a "dict" instead of the required element
My WebDriverWait is returning a "dict" instead of the required element

Time:11-02

I want to use Selenium with Gauge to grab data from a web page. I wrote the following code, using wait_for_element:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from step_impl.mixins.exception import MixinException

class MixinSelector(MixinException):

    def __init__(self, driver=None):
        super(MixinSelector, self).__init__(driver)
        self.wait_for_element_time = 60

    def wait_for_elem(self, by, locator_string):
        try:
            element = WebDriverWait(self.driver,self.wait_for_element_time)
                      .until(EC.presence_of_element_located((by, 
                       locator_string)))
            print(element)
            print(type(element))  
            return element
        except TimeoutException:
            self.fail_test('failed')
        except:
            raise

This, however, results in a dict instance, as shown by the debug prints:

{'ELEMENT': '0.8884832448581164-1'}
<class 'dict'>

I call the function like so:

self.wait_for_elem(By.CSS_SELECTOR, '#baseWebLayoutContainerID')

This is a problem because I need the result to be a web element I can use in several other functions. How can I get that result?

CodePudding user response:

Don't expect a web element getting back from WebDriverWait, instead put another line after WebDriverWait to get the web element, see below code

WebDriverWait(driver, 10).until(EC.presence_of_element_located((by, locator_string)))
element = driver.find_element(by, locator_string) 
return element

it works fine with me as you can see in the below image

enter image description here

  • Related