I am searching within a PowerBI dashboard with Selenium a certain pop-up window to insert a search term. I can only do this in stable way by creating a function which looks for all input fields and then try to send the term within a try-except-clause which takes very long.
Hence I am wondering if it is possible to insert a command how long to wait till an exception occurs.
Here is my code
fields_searchheader = browser.find_elements_by_css_selector(".searchHeader.show > .searchInput")
for i in fields_searchheader:
try:
i.clear()
i.send_keys("hallo)
# wait function till exception if no feedback
except Exception:
print("d")
Would be great to get an idea. Thanks!
I would recommend to you to use - Expected Conditions
And also to make it stable - tune it by using polling.
Something like this?
it will wait up to..not for the number of seconds..
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
def InputByCSS(NameOfObject, WhatToSend):
try:
item = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, NameOfObject)))
item.click()
item.send_keys(Keys.CONTROL, 'a')
item.send_keys(WhatToSend)
except TimeoutException as e:
print("InputByXPATH Error: Couldn't input by XPATH on: " + str(NameOfObject))
pass
fields_searchheader = browser.find_elements_by_css_selector(".searchHeader.show > .searchInput")
for i in fields_searchheader:
try:
InputByCSS(i,'hallo')
except Exception:
print("d")
Related
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)
This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 2 years ago.
This code used to work:
time.sleep(3)
driver.switch_to.window(window_facebook)
driver.find_element_by_xpath("//input[#name=\"email\"]").send_keys(fb_email)
driver.find_element_by_xpath("//input[#name=\"pass\"]").send_keys(fb_pass)
driver.find_element_by_xpath("//input[#name=\"login\"]").click()
driver.switch_to.window(window_tinder)
time.sleep(3)
driver.find_element_by_xpath("//span[contains(text(), 'Allow')]").click()
#driver.find_element_by_xpath("//span[contains(text(), 'ALLOW')]").click()
#driver.find_element_by_xpath("//button[#aria-label=\"Allow\"]").click()
time.sleep(3)
driver.find_element_by_xpath("//span[contains(text(), 'Not interested')]").click()
This used to work, but now it stopped working with the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(), 'Allow')]"}
driver.find_element_by_xpath("//span[contains(text(), 'Allow')]").click()
Screenshots:
https://i.imgur.com/IXJtRPdh.png
https://i.imgur.com/IJ0j6jXh.png
As per my comment above server might taking time to load your page. Try below code and check if its working for you.
Imports required
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
Code with WebDriverWait
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'Allow')]")))
element.click()
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
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')]"
Is there any way to set a property for ChromeDriver to stop loading if time exceeds 10 seconds?
Something like the following pseudocode:
WebDriver dr = new ChromeDriver();
dr.loadLimit(10 SECONDS);
PS implicit wait is not going to work because it waits for Chrome response and only then starts counting 10 seconds.
In Python: driver.set_page_load_timeout(10)
Since version 2.1 of selenium chromedriver it supports a page load timeout. Before that version it was missing. (See the discussion about this here)
This is how you would set this in Java:
int pageLoadTimeout = 10;
driver.manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
(see the webdriver spec and the selenium resources about this)
It work for me
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
desired_capabilities = DesiredCapabilities().CHROME
desired_capabilities['pageLoadStrategy'] = 'none'
d = webdriver.Chrome(desired_capabilities=spider.desired_capabilities)
d.get('http://www.google.com')
time.sleep(10)