Python - Selenium Webdriverwait NoSuchElementException does not get caught by except - selenium

So I have literally tried everything at this point.
I am getting the following error:
Error
The code that is causing the error is the below.
I have tried three difference excepts at this point, TimeoutException, NoSuchElementException and just a normal Exception, but neither works. If I uncomment the "raise NoSuchElementException" inside the try, it catches the error correctly, but not when it's the WebDriverWait that causes it?
Can someone please help :)
try:
#raise NoSuchElementException
WebDriverWait(driver, 15).until(lambda s: s.find_element_by_class_name("stream-preview__preview-image--loaded").is_displayed())
except TimeoutException:
print('NoSuchElementException:')
except NoSuchElementException:
print('NoSuchElementException')
except Exception as e:
print(e)

Related

How do I resolve the NoSuchElementException error in Selenium?

HTMLI was following an online tutorial on scraping glassdoor website via selenium.
My code does not get through this statement:
try:
driver.find_element_by_class_name("selected").click()
print('x out worked')
except ElementClickInterceptedException:
print('x out failed')
pass
time.sleep(.1)
try:
driver.find_element_by_css_selector('[alt="Close"]').click()
print(' x out worked')
except NoSuchElementException:
print(' x out failed (next page or missing)')
pass
The error I receive is :
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".selected"}
Couple of things that I tried:
driver.maximize_window()
driver.implicitly_wait(20)
Hard to tell without seeing the HTML, but try reversing your quotes (single/double)...
driver.find_element_by_css_selector("[alt='Close']").click()
Or...
driver.find_element_by_xpath("//*[#alt='Close']")
If that doesn't work add the relevant HTML section to your post

selenium.WebDriverException: Returned value cannot be converted to WebElement

Seeing below error when trying to find element. Here I want to fill text after finding web element.
I am able to find xpath through chrome console, some how I am getting this issue. There are some posts on this issue, however most of them are related to appium, which is irrelevant to me.
util.driver.switchTo().defaultContent();
util.driver.switchTo().frame(0);
util.driver.findElement(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input"));
Error message:
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {error=no such element, message=no such element: Unable to locate element: {"method":"xpath","selector":"//label[text()='Reason for Escalation']/following-sibling::div/input"}
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:324)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
There are 3 iframes on the page the elements am accessing are in first page
I would try to invoke WebDriverWait on the iframe before switching to it.
// wait for iframe to exist, then switch to it
WebDriverWait wait = new WebDriverWait(util.driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.Xpath("//iframe[contains(#name, 'vfFrameId')]")));
// wait for element to exist
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input")));

WebDriverWait throws Timeout instead of NoSuch ElementException

I am using WebDriverWait to find an Element which will be visible after few seconds.
I have declared time for 10sec max to wait for that particular element
WeDriverWait wait = new WebDriverWait(driver, 10)
.until(ExpectedConditions.visibilityOfElement("path"));
now my expection is to , if element is not visible withing 10 seconds then i should get NoSuchElementException after 11th second, but it takes more than 30secs(approx) and throws TimeOut Exception.
Thanks in advance for Suggestion and clarification ...!!!
You saw it right. As per the documentation of WebDriverWait() the constructors are:
WebDriverWait(WebDriver driver, java.time.Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut)
WebDriverWait(WebDriver driver, long timeOutInSeconds)
WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)
For a successful WebDriverWait the desired element/elements is/are returned, whereas incase of a failure timeout exception is thrown.
However there is a small issue in your code block:
WeDriverWait wait = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElement("path"));
Instead of an instance of WeDriverWait, the desired element is returned. So you need to change the line as:
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElement("path"));
In a stepwise manner:
WeDriverWait wait = new WebDriverWait(driver, 10)
WebElement element = wait.until(ExpectedConditions.visibilityOfElement("path"));
It is not clear from your question why it takes more than 30 secs(approx) to throw the TimeOutException but the most possible cause is, though you have set the duration of WebDriverWait as 10 seconds, you have also have induced ImplicitlyWait as well and as per the documentation WARNING: Do not mix implicit and explicit waits! Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.
As per WebDriverWait class source:
Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
the 'until' condition, and immediately propagate all others. You can add more to the ignore
list by calling ignoring(exceptions to add)
And NotFoundException is a super class for the following Exceptions:
NoAlertPresentException
NoSuchContextException
NoSuchCookieException
NoSuchElementException
NoSuchFrameException
NoSuchWindowException
Therefore you will not see NoSuchElement exception when using WebDriverWait.
It might also be the case your element is actually present in the DOM but it's not visible due to having i.e. display:none CSS property so you could consider using presenceOfElementLocated condition instead.
More information: How to use Selenium to test web applications using AJAX technology

selenium.common.exceptions.InvalidSelectorException using Selenium in python 3.7

I want to use selenium to automate the process from open a specific website to log in to search for particular articles. Few of the steps I could do it but facing error in 'sign in' step.
from selenium import webdriver
from selenium.webdriver.common.by import By
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH, '//button[#type="button"],[contain(.,"Sign In")]').click()
browser.find_element_by_id('username').send_keys('**#&^&#$##$')
browser.find_element_by_id('password').send_keys('###$%%**')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()
The code is working till this line:
browser.find_element_by_id('na,us').click()
But after that it is showing error in this line:
browser.find_element(By.XPATH, '//button[#type="button"],[contain(.,"Sign In")]').click()
The error message says​:
selenium.common.exceptions.InvalidSelectorException: Message:
What is wrong is my code?
This error message...
selenium.common.exceptions.InvalidSelectorException
...implies that the XPath expression was not a valid one.
However, it seems you were close. You need to replace:
'//button[#type="button"],[contain(.,"Sign In")]'
and join the two condition with and operator as follows:
"//button[#type='button' and contains(.,'Sign In')]"

Why Wait.until() doesn't work in Selenium WebDriver?

I have been using Selenium WebDriver. I want to wait until the element is present on the webpage, for which i am using:
WebDriverWait wait = new WebDriverWait(driver, Long.parseLong(timeout));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));
but my test get stucks in the second line if the element I am looking for, is not present and even beyond the timeout. Pls Help. Thanks.
Maybe:
- the element is present, so no exception is thrown
- then gets stuck because you are not doing anything else afterwards
Try printing smt after the until call. My guess is it will get printed.
Otherwise maybe it's the timeout:
It must be in seconds, not milli seconds.
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html#WebDriverWait(org.openqa.selenium.WebDriver, long)
I got it worked. I changed the timeout from 60000 to 60 since it takes the second argument as seconds and not millisecs.