Getting error while trying to automate web login using selenium web driver in python 3.6 - selenium-webdriver

Below is my code and getting this error only some times. Surprisingly, when i run the same code line by line, it is working as expected!
from selenium import webdriver
browser=webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.gmail.com')
email=browser.find_element_by_xpath('//*[#id="identifierId"]')
email.send_keys('my email-id')
next=browser.find_element_by_xpath('//*[#id="identifierNext"]/content/span')
next.click()
browser.implicitly_wait(9)
passwd=browser.find_element_by_xpath('//*[#id="password"]/div[1]/div/div[1]/input')
passwd.send_keys('mypassword')
next1=browser.find_element_by_xpath('//*[#id="passwordNext"]/content/span')
next1.click()
Error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <span class="RveJvd snByac">...</span> is not clickable at point (942, 426). Other element would receive the click: <div class="
ANuIbb IdAqtf" jsname="k4HEge" tabindex="0"></div>
(Session info: chrome=61.0.3163.91)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 x86_64)

Hope Your Problem will be solved
from selenium import webdriver
browser=webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.gmail.com')
email=browser.find_element_by_xpath('//*[#id="identifierId"]')
email.send_keys('my email-id')
next=browser.find_element_by_xpath('//*[#id="identifierNext"]/content/span')
next.click()
passwd=browser.find_element_by_name("password")
passwd.send_keys('mypassword')
next1=browser.find_element_by_xpath('//*[#id="passwordNext"]/content/span')
next1.click()

Below code should work out for you. The basic idea is to wait for the password field until its ready in DOM
from selenium import webdriver
browser=webdriver.Chrome()
browser.maximize_window()
browser.get('https://www.gmail.com')
email=browser.find_element_by_xpath('//*[#id="identifierId"]')
email.send_keys('my email-id')
next=browser.find_element_by_xpath('//*[#id="identifierNext"]/content/span')
next.click()
passwd=browser.find_element_by_xpath("//input[#name='password']")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(passwd))
passwd.send_keys('mypassword')
next1=browser.find_element_by_xpath('//*[#id="passwordNext"]/content/span')
next1.click()

Related

Why some of the elements cannot be selected by Selenium webdriver?

I've been using Selenium webdriver for quite a while. Recently I came across this page: https://www.myagedcare.gov.au/service-finder
It seem very simple and I try to use id locator: bylocation
However, no matter how long I wait for the element to be ready by adding wait for some time, or wait for element to be ready like:
WebDriver driver = getDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(
ExpectedConditions.elementToBeClickable(By.id("byname")))
.click();
the element is never ready, and I always get error like "no such element: Unable to locate element:"
What might be going on? I've used locator for quite a while and I do not know how this can happen.
I am using chromedriver 2.30 and Chrome browser v.60.
A simple id locator like this in www.google.com does not give me any trouble. I tested it so that I do not think it's driver or browser issue.
Logs
net.serenitybdd.core.exceptions.SerenityWebDriverException: Timed out after 10 seconds waiting for visibility of element located by By.xpath: //input[#id='bylocation']
Build info: version: '2.46.0', revision: '61506a4624b13675f24581e453592342b7485d71', time: '2015-06-04 10:22:50'
System info: host: 'yun-PC', ip: '192.168.1.14', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='bylocation']"}
(Session info: chrome=60.0.3112.50)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20.04 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Try this xpath and let me know if it works,
//div[#id='edit-search-by--2']//label[normalize-space(text())='Location']/preceding-sibling::input[#type='radio']
Also try waiting for visibility of element and not for element to be clickable.
Edit:
The above Xpath is perfect. Problem was that the element was located in a frame.
Below code will get you going with the click part:
driver.get("https://www.myagedcare.gov.au/service-finder?tab=help-at-home");
new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//iframe[#id='content']")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#id='content']")));
new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[normalize-space(text())='Location']/preceding-sibling::input[#type='radio']")));
WebElement location = driver.findElement(By.xpath("//label[normalize-space(text())='Location']/preceding-sibling::input[#type='radio']"));
WebElement name = driver.findElement(By.xpath("//label[normalize-space(text())='Name']/preceding-sibling::input[#type='radio']"));
location.click();
Thread.sleep(3000);
name.click();
driver.switchTo().parentFrame();
Click working video - https://www.screencast.com/t/0Fsw7gGZ
As the Radio Buttons are inside input tags, instead of id use xpath locator as follows:
WebDriverWait wait6 = new WebDriverWait(driver, 10);
WebElement radio_name = wait6.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='byname']/input[#id='byname']")));
radio_name.click();
Let me know if this Answers your Question.

WebDriverWait behaves differently when debugging

Started working with Selenium WebDriver. WebDriverWait is working fine when I run my tests, but I'm getting:
OpenQA.Selenium.NoSuchElementException
{"no such element: Unable to locate element:
{\"method\":\"id\",\"selector\":\"exampleInputEmail1\"}\n
(Session info: chrome=54.0.2840.71)\n
(Driver info: chromedriver=2.25.426923
(0390b88869384d6eb0d5d09729679f934aab9eed),
platform=Windows NT 10.0.14393 x86_64)"}
when I debug the tests.
Here's the code that fails:
Driver.Navigate().GoToUrl($"{BaseUrl}user/login");
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(3));
wait.Until(d => d.FindElement(By.Id("exampleInputEmail1")));
The last line throws. If I add a 30ms sleep, like so:
Driver.Navigate().GoToUrl($"{BaseUrl}user/login");
Thread.Sleep(30);
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(3));
wait.Until(d => d.FindElement(By.Id("exampleInputEmail1")));
it works in the debugger too. Why would they be different? I tried to search, but didn't find anything related to run v debug results. Thanks for any pointers.
Relevant details:
Visual Studio 2015
using Resharper 9.2 to run the tests under NUnit
referencing WebDriver and WebDriver.Support 3.0.0

a error occur in remoteDriver function of RSelenium

I want to scrape data on dynamic web by the following code:
> URL<- "http://www.cbooo.cn/realtime"
> library(bitops)
> library(RCurl)
> library(XML)
> library(RSelenium)
> library(magrittr)
> checkForServer()
Warning message:
checkForServer is deprecated.
Users in future can find the function in file.path(find.package("RSelenium"), "example/serverUtils").
The sourcing/starting of a Selenium Server is a users responsiblity.
Options include manually starting a server see vignette("RSelenium-basics", package = "RSelenium")
and running a docker container see vignette("RSelenium-docker", package = "RSelenium")
> startServer()
$stop
function ()
{
tools::pskill(selPID)
}
<environment: 0x10991af0>
$getPID
function ()
{
return(selPID)
}
<environment: 0x10991af0>
Warning message:
startServer is deprecated.
Users in future can find the function in file.path(find.package("RSelenium"), "example/serverUtils").
The sourcing/starting of a Selenium Server is a users responsiblity.
Options include manually starting a server see vignette("RSelenium-basics", package = "RSelenium")
and running a docker container see vignette("RSelenium-docker", package = "RSelenium")
> remDrv <- remoteDriver()
> remDrv$browserName="Internet Explorer"
> remDrv$open()
[1] "Connecting to remote server"
Selenium message: The best matching driver provider org.openqa.selenium.ie.InternetExplorerDriver can't create a new driver instance for Capabilities [{nativeEvents=true, browserName=Internet Explorer, javascriptEnabled=true, version=, platform=ANY}]
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'DESKTOP-J0D980N', ip: '10.36.17.76', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_77'
Driver info: driver.version: unknown
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: org.openqa.selenium.WebDriverException
Further Details: run errorDetails method
There are the following questions which I can't solve:
1 checkForServer,startServer are deprecated.
2 connecting to remote server always fials,I don't konw how to set some argurment in this funciotn and what should be done
I hope to get a anwser as soon as possible ,thanks.
The author of RSelenium provides a solution as following (https://github.com/ropensci/RSelenium/issues/81):
From Firefox 48 on-wards the gecko driver/ marionette will be needed to run Firefox with Selenium.
If you have Firefox 48 you can run the gecko driver as follows:
Refer to the guidelines
https: //developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver
Download the relevant gecko driver from
https: //github.com/mozilla/geckodriver/releases
Add it to your PATH or refer to the location when starting binary (see below)
# get beta selenium standalone
RSelenium::checkForServer(beta = TRUE)
# assume gecko driver is not in our path (assume windows and we downloaded to docs folder)
# if the driver is in your PATH the javaargs call is not needed
selServ <- RSelenium::startServer(javaargs = c("- Dwebdriver.gecko.driver=\"C:/Users/john/Documents/geckodriver.exe\""))
remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE))
remDr$open()
....
....
remDr$close()
selServ$stop()
Well to produce a viable working solution I would use the old version of RSelenium and everything with this code.
if (!require("XML")) {
install.packages("XML",repos= 'https://cloud.r-project.org')
library("XML")
}
#XML is a dependency
if (!require("RSelenium")) {
install.packages("https://cran.r-project.org/src/contrib/Archive/RSelenium/RSelenium_1.3.5.tar.gz", repos=NULL, type="source", dependencies = TRUE)
library("RSelenium")
}
download.file('http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar', destfile = "~/Documents/R/library/RSelenium/bin/selenium-server-standalone.jar")
#start server
system('java -jar "~/Documents/R/library/RSelenium/bin/selenium-server-standalone.jar"')
library(RSelenium)
checkForServer()
startServer()
That is not an optimal solution. But a working solution.

browser.get(URL) does not work

I am having an issue with entering text into the address bar nothing happens and I get the error
selenium.common.exceptions.WebDriverException: Message: Can't load the
profile. Profile Dir:
/var/folders/8_/f48lnzrs7r59_h2yb3dkfbh40000gn/T/tmpsGffLD If you
specified a log_file in the FirefoxBinary constructor, check it for
details.
I am able to open the browser but when I uncomment the code browser.get('http://www.google.com') nothing happens and I get the error listed above. I am using Firefox 48, Python 2.7.10 and selenium-2.53.6-py2.7.egg
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
Change that line to
browser.url('http://www.google.com')
OR
browser.open('http://www.google.com')

Using PhantomJS through Selenium with Python - WebDriverException error

I am using the following code, as recommended here: Is there a way to use PhantomJS in Python?.
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768) # optional
driver.get('https://google.com/')
driver.save_screenshot('screen.png') # save a screenshot to disk
sbtn = driver.find_element_by_css_selector('button.gbqfba')
sbtn.click()
When I try to run it I get the following error:
WebDriverException - "Unable to start phantomjs with ghostdriver."
Apparently, this error can be solved by replacing the 3rd line with:
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
I'm using Windows7, and the path for me appears to be: "C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\bin\phantomjs". I have tried using this and variations of it as the executable_path, but I still get the same error. I have also tried adding the folder location to the path. Nothing has worked. I suspect that I am missing something pretty obvious.
Aha!
It was a slightly different location. The line I was looking for was:
driver = webdriver.PhantomJS(executable_path=r'C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\lib\phantom\phantomjs')

Resources