Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix erro in webelemet not created, verifing if is str or dict. #3481

Merged
merged 4 commits into from
Apr 3, 2020
Merged

Fix erro in webelemet not created, verifing if is str or dict. #3481

merged 4 commits into from
Apr 3, 2020

Conversation

reinaldorossetti
Copy link
Contributor

@reinaldorossetti reinaldorossetti commented Feb 7, 2017


This change is Reviewable

fix the problem when the element is string  or dict.
Reducing the impact of change.
@reinaldorossetti reinaldorossetti changed the title Reducing the impact of change inside this function _element_if_visible. Fix erro in webelemet not created, verifing if is str or dict. Feb 7, 2017
@reinaldorossetti
Copy link
Contributor Author

reinaldorossetti commented Feb 7, 2017

Fix error in webelemet not created, verifing if is str or dict. Tested in Firefox 51.0.1 e Chrome 55.

@@ -95,6 +95,8 @@ def __call__(self, ignored):


def _element_if_visible(element, visibility=True):
if isinstance(element, str) or isinstance(element, dict):
raise StaleElementReferenceException("Invalid locator")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right exception class. There is an InvalidSelectorException class that covers bad locators.

Copy link
Contributor Author

@reinaldorossetti reinaldorossetti Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have problem because element not is webelement is str in firefox and dict in chrome.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<selenium.webdriver.remote.webelement.WebElement (session="49274937103ea6cd11bc1c9a3a4784ee", element="0.9158218795761794-1")>
<selenium.webdriver.remote.webelement.WebElement (session="49274937103ea6cd11bc1c9a3a4784ee", element="0.9158218795761794-2")>
Traceback (most recent call last):
File "D:\Dropbox\Dropbox\Meu Curso de Selenium\testes\loading_page.py", line 30, in
test = wait_element_not_present(driver, ".loadingoverlay")
File "D:\Dropbox\Dropbox\Meu Curso de Selenium\testes\loading_page.py", line 20, in wait_element_not_present
expected_conditions.invisibility_of_element_located((By.CSS_SELECTOR, elem))
File "C:\Python35-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 198, in call
return _element_if_visible(_find_element(driver, self.locator), False)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 100, in _element_if_visible
return element if element.is_displayed() == visibility else False
AttributeError: 'dict' object has no attribute 'is_displayed'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that happening? That function is a 'private' function that should only be called when there's an element to examine from the public expected condition functions.

Copy link
Contributor Author

@reinaldorossetti reinaldorossetti Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This InvalidSelectorException not work because function need return true, do you look exception:

class invisibility_of_element_located(object):
""" An Expectation for checking that an element is either invisible or not
present on the DOM.

locator used to find the element
"""
def __init__(self, locator):
    self.locator = locator

def __call__(self, driver):
    try:
        return _element_if_visible(_find_element(driver, self.locator), False)
    except (NoSuchElementException, StaleElementReferenceException):
        # In the case of NoSuchElement, returns true because the element is
        # not present in DOM. The try block checks if the element is present
        # but is invisible.
        # In the case of StaleElementReference, returns true because stale
        # element reference implies that element is no longer visible.
        return True

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you test if I'm wrong or right? How should I make the above code work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested the code you wrote on a simple web page. What you wrote seems like it should work, except in the case of waiting for an element to go 'invisible' you should not care about the result (The result will be True for invisibility). Maybe you should go to the #selenium chat room on freenode IRC and ask for help there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This problem only happen in invisibility_of_element_located, no have problem that return true or false, but the problem is that it throws an exception in the code and stop my test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I know if the problem came from chrome or firefox? How do I test this?

@isaulv
Copy link
Contributor

isaulv commented Feb 7, 2017

I don't think this is the right level to put a type check; what problem are you trying to solve?
Also I wouldn't check on particular types, I would check if the variable element is not of instance WebElement.

NoSuchElementException adding
@barancev barancev added the C-py label Sep 6, 2017
@CLAassistant
Copy link

CLAassistant commented Nov 23, 2019

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ AutomatedTester
❌ reinaldorossetti
You have signed the CLA already but the status is still pending? Let us recheck it.

@AutomatedTester AutomatedTester merged commit b4b361c into SeleniumHQ:master Apr 3, 2020
@isaulv
Copy link
Contributor

isaulv commented Apr 3, 2020

@AutomatedTester I don't think this should have been merged. Clearly the function expects a web element type to have the is displayed function and it throws an exception. Either we add type checking for all expected condition functions for consistency and safety, or let python do its thing.

That being said I have seen cases were Chrome or Firefox does not return a proper WebElement object but that's probably a bug in the driver.

AutomatedTester added a commit that referenced this pull request Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants