Element not clickable using Selenium webdriver with Chrome (same code:works fine using firefox) - google-chrome

Using selenium-2.44.tar.gz and chrome for automating test cases:Very strange my code works fine using Firefox 33 but fails using Google Chrome:
"WebDriverException: Message: u'unknown error: Element is not
clickable at point(57, 161). Other element would receive the click: ...\n
(Session info: chrome=42.0.2311.135 )\n (Driver info:
chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)'"
self.driver.find_element_by_xpath(".//[#id='searchMessagstoryBtn']").click()
any idea? shouldnt it work better with chrome one wonders since webdriver is Google code nowdays or am I wrong!!!

Unlike firefox, to use chromedriver you have to download chromedriver from http://www.seleniumhq.org/download/ and had to give path in your code.
You can use the below code, to use chrome driver in java or in python
Java:
public void testGoogleSearch() {
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com/xhtml");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
Python:
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

It seems to be a bug in ChromeDriver.
http://code.google.com/p/selenium/issues/detail?id=2766
Try this workaround solution from form #27. I hope it would help you:
I ran into this same issue as well with Chrome...clicking the element
works fine in firefox but not in Chrome...the fix is pretty easy
though, all you have to do is scroll the element into view before
clicking it, and you won't run into this problem in Chrome. Here's the
code i use:
IWebElement elementToClick = ;
// Scroll the browser to the element's Y position (driver as
IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,
{0});", elementToClick.Location.Y));
// Click the element elementToClick.Click();
Hope this helps anyone else who runs into this issue

#FindBy(xpath = "//[#id='searchMessagstoryBtn']")
private WebElement Search_btn;
public WebElement getSearchBtnClick() {
return Search_btn;
}
public void Click_Search_Btn() {
//click the search button
TimeUnit.SECONDS.sleep(3);
getSearchBtnClick().click();
}

Related

Open universal links (deep links) with XCUITest

I would like to create several tests for native iOS application. To be more precise, I want to test deep links. But I am not sure how to trigger deep link with XCUITest and I don't really see how launch() and launcArguments (https://developer.apple.com/documentation/xctest/xcuiapplication) can help me. Did anybody have a chance to open deep link with XCUITest?
In iOS 14 using Spotlight seems to work well:
private func open(_ urlString: String) {
XCUIDevice.shared.press(.home)
XCUIApplication(bundleIdentifier: "com.apple.springboard").swipeDown()
let spotlight = XCUIApplication(bundleIdentifier: "com.apple.Spotlight")
spotlight.textFields["SpotlightSearchField"].typeText(urlString)
spotlight.buttons["Go"].tap()
}
Set safari as app like this
let safari = XCUIApplication(bundleIdentifier: "com.apple.mobilesafari")
Open your email in safari
Tap on the link
Normally assert some element on app
I never tried this, but this idea comes to my mind. Create a new dummy project/application that should only contain some links pointing to URLs that you expect your original app to open. From that new application, write some UI tests that tap a link, like this:
func testOpeningLinks() {
let app = XCUIApplication()
app.links["Some link text"].tap()
// This is the place where your original app should be opened...
// Find the XCUIApplication object:
let originalApp = XCUIApplication(bundleIdentifier: "original.app.bundle.identifier")
// You should be able to find some views from original app from here, eg. a button:
let button = originalApp.buttons.element
}
This would work only if you have previously installed your app on the device/simulator where you're running your UI tests.

Selenium cannot find element

Having some issues using Selenium web driver in Chrome.
My goal is to give the user ~15-30 seconds to log in on there own and then start doing automated testing.
The problem is after I click the login button and go to the next page, I am not able to find elements by xpath, id, etc.
public static void runTest() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.url.com");
driver.manage().window().maximize();
Thread.sleep(15000);
driver.findElement(By.xpath("//*[#id=\"content-main\"]/div/div/form/div/p/input")).click();
System.out.println("User has logged in and it has found element for Attachment Upload.");
Thread.sleep(15000);
driver.findElement(By.xpath("//*[#id=\"invoiceMenu\"]/a")).click();
}
I have also tried using explicit waits and have had no luck for example:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
The error I am usually getting back is:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //*[#id="invoiceMenu"]/a (tried for 10 second(s) with 500 milliseconds interval)
Edit:
Was able to get some of the elements (angular ones) working with a few plugins for Chrome. Element Locator and ChroPath worked fantastic. Took some playing around with, but once I got one I was able to piece together the rest of them.
I would suggest that you open the chrome console on your browser and try to interact with the element in question e.g. using:
document.getElementById('someId').click()
If you are able to click on the element like that, then you can use javascript executor in your code as follows:
((JavascriptExecutor)driver).executeScript("document.getElementById('someId').click();");

SendKeys(Keys.Tab) does not work on Chrome Version 53.0.2785.116

Today I observed that SendKeys(Keys.Tab) does not working on Chrome webdriver. However it works perfectly on IE and FireFox. Other than Tab, i tried few other keys like Space, Backspace, Clear, some text, Enter etc working as expected on Chrome.
for example:
private IWebDriver driver;
private string baseURL;
driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://accounts.google.com/SignUp?");
driver.FindElement(By.Id("FirstName")).Clear();
driver.FindElement(By.Id("FirstName")).SendKeys(Keys.Tab);
Anybody know why, Keys.Tab won't working on latest Chrome Version 53.0.2785.116?
OS: Windows 7 Service Pack 1
The cursor suppose to move/focus to Last name text field
try to add code below at the end of your code:
var element = driver.FindElement(By.Id("FirstName"));
if (!element.Equals(driver.SwitchTo().ActiveElement())) return;
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].blur();", element);
It means if the cursor still focus on current element, trigger blur function to move to next element.

Should all elements return true on isDisplayed() after a selenium Webdriver get()?

WebDriver get() and isDisplayed() methods are not working as I expected.
As for the doc:
This is done using an HTTP GET operation, and the method will block
until the load is complete
As mentioned in other questions like this Wait for page load in Selenium , the get method should wait for the page to load.
But after running get(), isDisplayed() method from RenderedWebElement does not always return true on some elements.
What are the possible causes?
I'd like some elaboration on the difference between being loaded and being displayed in the context of webdrivers.
In the latest UI frameworks/APi you can hide an element on a page.
For ex. Consider a page having 5 elements. When page is loaded only 3 elements will be shown on the page, the other 2 will be hidden and on taking some action the other 2 elements will be shown.
An example you can check under the demo section in following links:
Show element link: http://api.jquery.com/show/
Hide element link: http://api.jquery.com/hide/
When you use webDriver get() method, webdriver will wait for page to load i.e. it waits for all the html content of the page to be loaded onto the browser. This does not means that all the elements are visible.
When you use isDisplayed() webdriver checks whether the said element is Displayed on the page or not. If you know that the element may be hidden on the page when running your test-cases its a good approach to verify whether the element is displayed or not. Else your test script fails with a error "Element not displayed to take an action"
Hope this helps.
isDisplayed() seems to me not so good approach. get some ideas from wait: exlplicit and implicit wait mechanisms:
Explicit wait
WebDriverWait.until(condition-that-finds-the-element)
Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Waits:
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition<WebElement>(){
#Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Implicit Waits:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
The example what you have given both do exact same thing..
In Explicit wait, WebDriver evaluates the condition every 500
milliseconds by default ..if it is true, it comes out of loop
Where as in ImplicitWait WebDriver polls the DOM every 500
milliseconds to see if element is present..
Difference is
1. Obvious - Implicit wait time is applied to all elements in your
script but Explicit only for particular element
2. In Explicit you can configure, how frequently (instead of 500
millisecond) you want to check condition.
3. In Explicit you can also configure to ignore other exceptions than
"NoSuchElement" till timeout..
you can get some more info here
Also I use fluent wait mechanism for waiting elements get rendered on the page. Actually you pass either css selector either xpath to the function and simply get web element.
public WebElement fluentWait(final By locator){
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
}
);
return foo; } ;
fluent wait description
hope this comes clear now)

Selenium WebDriver not working for Html5 drag and drop

Below is the code I am using, it will access one public website which is written using Html5,the code is trying to drag the "One" to trashbin, but it doesn't do anything. Is there any workaround to make it work? Thanks in advance!
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://html5demos.com/drag");
WebElement draggable = driver.findElement(By.id("one"));
WebElement droppable = driver.findElement(By.id("bin"));
new Actions(driver).dragAndDrop(draggable, droppable).build().perform();
Additional information is:
Selenium version: 2.25.0
OS: Mac OS Lion
Browser: Firefox
Browser version: 10.0.2 and 14.0.1
I would risk saying it is some sort of compatibility issue. I used the following code on http://jqueryui.com/demos/draggable/
System.setProperty("webdriver.ie.driver","C:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/demos/draggable/");
WebElement draggable = driver.findElement(By.id("draggable"));
new Actions(driver).dragAndDropBy(draggable, 200, 10).build().perform();
and it worked like a charm on the IE9. However it did not work with my Firefox 14 which may be to "new".
You can also try some workaround using JavaScriptExecutor, sth like this is described here: HTML5 Drag and Drop using Selenium Webdriver for Ruby