Selenium with Firefox not clicking on elements located using XPath - java

I have a Selenium Java based automation infrastructure. Most elements locators are using XPath since our web site uses several templates where elements can be located based on their texts only.
It is working just perfect on Chrome however it doesn't work on Firefox.
The most basic action - element.click() doesn't work with Selenium on Firefox while it works perfect on Chrome!
There is no exceptions thrown, just nothing done.
I do not use any kind of very complex xPath expression, mostly something like //*[contains(text(),'Some unique text')] that gives a clear unique element locator and each click() is performed after getting the element visible with methods like this:
public void clickVisible(String xpath){
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
driver.findElement(By.xpath(xpath)).click();
}
While running and debugging the tests I saw that elements are found visible shortly while running on Firefox too, however click() simply doesn't work there.
In case I locate those elements with css selector it works on Firefox perfect!
I already heard that xPath engines are different in each browser and I saw several old posts here dealing with xPath related problems, but mostly with IE, not with Firefox, and most of that posts are written many years ago.
Also, I worked with Firefox several years ago and there were no such problems that time!
I'm trying to understand what happened with Selenium on Firefox so it is not performing very basic commands for xPath located elements?
I couldn't find any clear documentation / explanation about this.
This post looks interesting, however I couldn't try setting capability.setCapability("marionette", false); since my Firefox options are set in the following manner:
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("browser.download.dir",downloadsPath);
--------
options.setProfile(profile);
driver = new FirefoxDriver(options);

Are you running the test in headless mode. If so you have to maximize the screen by setting it in environment variables.
os.environ['MOZ_HEADLESS_WIDTH'] = '1920'
os.environ['MOZ_HEADLESS_HEIGHT'] = '1080'
If not headless please update the firefox options to the below size to fix the issue.
firefoxOptions.AddArgument("--width=1920");
firefoxOptions.AddArgument("--height=1080");
Arjun

Related

Headless Chrome - getting blank page source

I'm trying to load a website with Chrome browser in headless mode using Selenium web driver. I face an issue with some specific websites. The page is loading, in the first 2-3 seconds it shows a page with "please enable javascript..." and after 3 seconds, page source goes blank.
I'm using Selenium and especially Chrome for long time and I am familiar with the platform. For the purpose of this case, I'm using Chrome Version 73.0.3683.86 , ChromeDriver 2.46.628411 (which is compatible according to Which ChromeDriver version is compatible with which Chrome Browser version?) on a Mac OS. selenium java version is latest - 3.141.59
I suspect that headless Chrome cannot handle specific content-type such as "svg" and any other GUI related HTTP response.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://identity.tescobank.com/login");
Thread.sleep(3000);
System.out.println(driver.getPageSource());
driver.quit();
Expected result is to have the page source same as it is showing in non-headless mode.
Headless Chrome should be able to handle everything the normal Chrome can do:
It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
(see https://developers.google.com/web/updates/2017/04/headless-chrome)
Since only the login page of a bank causes you trouble, my guess is that the security of the page detects an anomaly and decides not to serve you.
One way they can do that is by looking at the User Agent string which contains HeadlessChrome.
That said, unless you're writing integration tests for the bank, your behavior is at least suspicious. If you have a valid and legal concern, clear it with the bank first. They might take actions against you, otherwise. Blocking your IP address (which could affect many people) or asking the police to have a word with you.
I was facing similar issue in my script, after login. Somehow refreshing the page resolved the issue.
driver.navigate().refresh();

Selenium Chrome Driver Limitations Web Scraping at Scale

I'm planning to use Selenium Chrome Driver for my project which will be used to do web scraping to multiple public websites (something like kayak or skyscanner). So there will be a REST GET endpoint where my backend would launch headless Chrome to scrape multiple websites, and eventually return a manipulated JSON.
I want to know how scalable is Chrome Driver as it sounds like a headless Chrome instance needs to be launched whenever a request comes in.
Updated: Question using Google Chrome Headless
Please find the pros and cons of phantom js which I noticed during implementation .Hope this helps.
Cons:
1)It will fail to recognize the browser elements like id,xpath,csselector
when compared to chrome driver.
2)If you have login mechanism ,redirects won't work as you expect when compared to chrome driver.
3)You need to manually implement the custom logic for screen shots for the test failures if you need it.
4)If you want to switch between multiple drivers like chrome,html etc then it is very difficult
Pros:
1)Test case execution is faster when compared to chrome driver
2)No browser is required it will run without GUI.
3)No much configurations are needed when compared to chromedriver.
You can go with html driver also which is quite faster then phantom but even it has its own limitations that you need take care of before implementation.
I am not sure that you really need to use PhantomJS.
Chrome implemented "headless" mode couple of months ago.
"Headless Chrome" does the same job that PhantomJS, and does it better.
I heard that PhantomJS authors even said that they will not support it anymore.
You can enable headless mode in Selenide with just on line:
Configuration.headless = true;
Did you think about headless chrome?
Headless Chrome

Java Phantomjs behaving different in windows and linux

I been trying to solve this issue but not sure what's the cause for this,
i made a program with java and phantomjs, and in theory they both should behave the same way.
Phantomjs in both the pc and the server is the same v.2.0.
In windows(v.7) mi testing program works as expected, but in linux (debian) the program fails, with this error when it tries to click an element:
Caused by: org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with css selector
Now, i know sometimes we have to wait for few seconds for the page to load, which already do (even added a few more just to be sure) and it was tested correctly in windows.
i tried with the click function:
element.click();
with a javascript code:
JavascriptExecutor js = (JavascriptExecutor)driver;
js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element)
and with actions too:
Actions action = new Actions(driver);
action.moveToElement(element).contextClick().build().perform();
//and this code too
action.moveToElement(element).click().build().perform();
Anyone had this same or similar issue? what i else i can do? i'm stuck since yesterday and i wan't able to figure out this problem.
note: for now i have an alternate way to get page without having interaction, but still having different results from the web page when executing in different OS, for example:
where i normally i get this link: www.somesite.com/?search=xxxxx&date_in=dd/MM/yyyy&params=etc. , instead i get www.somesite.com/?search=xxxxx, the default search without any especific search parameters.
You need to use onResourceError callback, to find out what's going wrong.
You also need to use the following Command-Line options:
--ignore-ssl-errors=true --ssl-protocol=any --debug=true
See also: this issue.

selenium testcase failing with IE browser while it's working fine with chrome and firefox

While I am executing testcases (Selenium WebDriver + Java) on Internet explorer i am getting exception "unhandledAlertException" and testcases getting failed while it's working fine with Chrome and Firefox browser. Also having issue like: sometimes it click on element and respond accordingly and sometimes its clicked but did not respond with same code.
Here is some code from where it was throwing "unhandledAlertException"
protected void clickOnElement(WebElement element)
{
if(isElementPresent(element))
{
if(getBrowserName().equalsIgnoreCase("internet explorer")){
element.sendKeys(Keys.ENTER);
wait(2000);
System.out.println("In IE clicked on element");
}else{
element.click();
}
implicitWait(Integer.parseInt(ConfigParser.getProperty("common_wait_timeout")));
}
}
unhandledAlertException is now resolved by removing implicit wait from code. but sometimes unresponsiveness of elements in IE is still persist.
How can overcome from such issues with IE
Try adding proper wait conditinos in your code for IE browser. Generally IE doesnt respond well. Also have a look at the element Id's in IE they might be changing.
IE will always have issues with selenium webdriver. Microsoft itself doesn't maintain IE driver for selenium like chrome and FF. You best chances are to use proper wait.
What version of IE are you using? There are known issues with the latest versions of IE with Selemium. I faced similar issues and had to switch back to IE8. Also are you getting the exception after the browser is launched? Tip: Under IE options, make sure all the modes are enabled under the security tab and the zoom is set to 100%.
Hope this helps.. good luck!!
if (browser.equalsIgnoreCase(IE)) {
System.setProperty("webdriver.ie.driver", getRootDir() + "/src/main/java/configuration/IEDriverServer.exe");
driver = new InternetExplorerDriver();
you have to download IEDriverServer.exe and set the path in the above, and then get the driver of IE, it will work fine and i hope you will get any problem if it is even IE 11/click problem.

Chrome WebDriver doesn't select from dropdown

I have got the following code working on Firefox, but not on Chrome.
Select selectSize = new Select(dropDownListBox);
selectSize.selectByIndex(1);
I've tried using explicit wait and Thread.sleep(), but couldn't make it work.
When I debug the code, I found out that it actually grabs the value, but doesn't update the UI.
When I use System.out.println(selectSize.getFirstSelectedOption().getText()); it actually prints the size.
I've also tried running this code on iPhone and iPad emulators and it worked on there as well. I feel like this only doesn't work with Chrome Driver.
Has anyone had this issue before or do you have any suggestions?
Update: I'm using Selenium 2.41.0 and Chrome WebDriver 2.9.
Thanks in advance

Categories

Resources