While opening Gmail, following error occurs- Expected [object Undefined] undefined to be a string - selenium

Expected [object Undefined] undefined to be a string,
The code I am using is following:
System.setProperty("webdriver.gecko.driver","E:\\Software\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
//wd.findElement(By.xpath(".//*
[#id='gbw']/div/div/div[1]/div[1]/a")).click();
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("abc#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Ne
xt']")).click();
error log
error log
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.firefox.GeckoDriverService.access$000(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.usingFirefoxBinary(GeckoDriverService.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:204)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:104)
at register_prctc.gmail.main(gmail.java:15)

Here is the solution to your Question:
To work with Selenium 3.4.0, geckodriver v0.16.1 & latest Mozilla Firefox 53.x you need to set the absolute path of the geckodriver in your code as:
System.setProperty("webdriver.gecko.driver","C:\\your_dir\\geckodriver.exe");
As per best practices you should not use Thread.sleep(6000), instead use ImplicitlyWait or ExplicitWait.
The xpath .//[#id='gbw']/div/div/div[1]/div[1]/a you used doesn't identifies any unique element. To find the element Gmail link you can use the linkText locator as:
wd.findElement(By.linkText("Gmail")).click();
For sending text into Email or Phone field provide a unique xpath as:
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
The xpath to click on Next button looks vulnerable to me, you may like to change it to : wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Here is the working set of your own code with some simple tweaks:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
WebDriver wd= new FirefoxDriver();
wd.get("https://www.google.co.in/");
wd.findElement(By.linkText("Gmail")).click();
WebElement e1= wd.findElement(By.xpath("//input[#id='identifierId']"));
e1.sendKeys("id#gmail.com");
wd.findElement(By.xpath("//div[#id='identifierNext']/content/span[text()='Next']")).click();
Let me know if this Answers your Question.

Remove Selenium-java-2.53.1.jar file and update all jars

Related

Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12

I'm attempting to replicate this code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://inventwithpython.com')
try:
elem = browser.find_element_by_class_name(' cover-thumb')
print('Found <%s> element with that class name!' % (elem.tag_name))
except:
print('Was not able to find an element with that name.')
But it keeps returning the exception. I'm running this on mac w. vscode and there are few things off.
find_element_by_class_name method doesn't seem to register as a method.
Everytime I run this Intellicode prompts get disabled
I can't run this at all on Chrome as it crashes the chrome browsers
I've also searched online for driver issues and have done webdriver with the chrome driver path. Didn't work either
This is the error I'm getting if run it without try and except.
elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
Selenium removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* ...
You now need to use:
driver.find_element("class name", "VALUE_OF_CLASS_NAME")
Eg:
driver.find_element("class name", "cover-thumb")
But if a class name has multiple segments, use the dot notation from a CSS Selector:
driver.find_element("css selector", "img.cover-thumb")

Selenium find_element failing to get radio with xPath [duplicate]

I'm trying to play QWOP using Selenium on Chrome but I keep getting the following error:
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element
{"method":"id","selector":"window1"
(Session info: chrome=63.0.3239.108
(Driver info: chromedriver=2.34.522913
(36222509aa6e819815938cbf2709b4849735537c), platform=Linux 4.10.0-42-generic x86_64)
while using the following code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
browser = webdriver.Chrome()
browser.set_window_size(640, 480)
browser.get('http://www.foddy.net/Athletics.html?webgl=true')
browser.implicitly_wait(10)
canvas = browser.find_element_by_id("window1")
canvas.click()
while (True):
action = ActionChains(browser)
action.move_to_element(canvas).perform()
canvas.click()
canvas.send_keys("q")
The same code works perfectly on Firefox, but because I want to use chrome's capability to run an webgl game in headless mode I can't really switch to Firefox.
Any workarounds to get this working?
NoSuchElementException
selenium.common.exceptions.NoSuchElementException popularly known as NoSuchElementException is defined as :
exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
NoSuchElementException is basically thrown in 2 cases as follows :
When using :
webdriver.find_element_by_*("expression")
//example : my_element = driver.find_element_by_xpath("xpath_expression")
When using :
element.find_element_by_*("expression")
//example : my_element = element.find_element_by_*("expression")
As per the API Docs just like any other selenium.common.exceptions, NoSuchElementException should contain the following parameters :
msg, screen, stacktrace
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[#id='create-portal-popup']/div[4]/div[1]/button[3]"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)
Reason
The reason for NoSuchElementException can be either of the following :
The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".
The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.
The WebElement you are trying to locate is within an <iframe> tag.
The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use execute_script() method to scroll the element in to view as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :
driver.switch_to.frame("iframe_name")
driver.switch_to.frame("iframe_id")
driver.switch_to.frame(1) // 1 represents frame index
Here you can find a detailed discussion on How can I select a html element no matter what frame it is in in selenium?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with expected_conditions set to proper method as follows :
To wait for presence_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
To wait for visibility_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
To wait for element_to_be_clickable :
element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
This Usecase
You are seeing NoSuchElementException because the id locator doesn't identifies the canvas uniquely. To identify the canvas and click() on it you have to wait for the canvas to be clickable and to achieve that you can use the following code block :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[#id='window1']"))).click()
Reference
You can find Selenium's java client based relevant discussion in:
NoSuchElementException, Selenium unable to locate element

Unable to find an exact match for CDP version 98, so returning the closest version found: 97

this is the screenshot of my first test case in selenium
pls help me with this what error its there as I am new to selenium
Try using something like this:
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
// click on the compose button as soon as the "compose" button is visible
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}
Source: https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver
Your error is caused by trying to Click on something that the webdriver has not found, giving it time to load the webpage will help.

Not able to write in inputbox of Google's homepage by WebDriver

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
I want to send to use xpath as a locater.
Error:
Exception in thread "main"
org.openqa.selenium.InvalidElementStateException: Element is disabled
and so may not be used for actions Command duration or timeout: 75
milliseconds
After that I want to get the google suggestions printed.
You can try using name instead of XPATH. Name selector is faster than Xpath.
driver.findElement(By.name("q"));
You are trying to access the google search bar by ID, try this id: //*[#id='lst-ib']
driver.findElement(By.xpath("//*[#id='gs_htif0']"))
.sendKeys("selenium");
You can also try these:
python: driver.find_element_by_xpath("//*[#name='q']").send_keys('selenium')
java: driver.findElement(By.xpath("//*[#name='q']")).sendKeys("selenium");
To get the suggestions you can try this:
List<WebElement> rows = driver.findElements(By.xpath("//*[#role='option']"));
for (int i=0; i<rows.size(); i++){
System.out.println(rows.get(i).getText());
}
Output: selenium selenium ide selenium tutorial selenium webdriver
First click in the input box and then use . sendKeys("whatever you want to type ")

CKEditor + Selenium WebDriver not work with FireFox 9/10 anymore

Before moved to FF9/10, this line sendKeys can work well with FF8.0.1 + Selenium 2.18. But after upgrade FF to 9 and 10, sendKeys will get nothing in CKEditor. No exception, no warning. I am wondering if it is the bug of CKEditor? or FF 9/10? or WebDriver? Anyone have clue for this?
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setPlatform(Platform.WINDOWS);
URL remoteAddress = new URL("http://127.0.0.1:4444/wd/hub");
RemoteWebDriver driver = new RemoteWebDriver(remoteAddress,desiredCapabilities);
driver.get("http://ckeditor.com/demo");
WebElement element = driver.findElement(By.id("cke_contents_editor1")).findElement(By.tagName("iframe"));
element.sendKeys("Cheese!");
Of course, I can access the instance of CKEditor directly like below, but it just a workaround.
((JavascriptExecutor) concorddriver).executeScript("CKEDITOR.instances.editor1.insertText( 'hello' );");
If sendKeys() doesn't work, try a click() before using it.
This often helped on similar issues.
But before you try this, check if the element is really found!
If not check if the element's id is still the same and/or try to use other By. methods (like By.className(),...)