First the specs, I am writing Selenium Webdriver tests in java for an application written largely in ExtJS and running on the firefox browser v14. The interesting thing is that Selenium can find the element that I want to click, but the click does not seem to be executed or if it is getting executed the desired outcome (a popup appearing) does not happen. I have also verified in Selenium IDE that the element I am looking for (a span element that I locate via Xpath) exists, but in Selenium IDE I run into the same issue of not being able to click on it.
If I manually click on the button the popup window appears asking for what file I want to upload. I have also tried other elements such as the span's parent 'button' element and parent 'em' element and parent 'div' element all with no luck.
What kills me is that I have been writing Selenium Tests for this application for weeks now and always been able to use this method to click on buttons and for this particular button it no longer works.
WebElement uploadButton = driver.findElement(By.xpath("//span[contains(text(), 'Upload Popup')]"));
uploadButton.click();
Edit 1: Thought that the code of the button itself might help
<button id="filefield-1158-buttonEl-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="background-color: transparent;">
Note the id is the dynamic id created by ExtJS
It can be because of lots of reasons. I would suggest that you debug while your test is running.
It is possible to install FireBug in your Selenium Firefox instance:
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);
I assume you can launch your test via JUnit (in an IDE like Eclipse). Launch your test in debug mode and set a breakpoint just before clicking on the button. Inspect the html code via FireBug then. It might give you a start.
Another posibility is to select the button by css class (By.className) or selector (By.cssSelector):
WebElement uploadButton = driver.findElement(By.className("x-btn-center"));
uploadButton.click();
If there are multiple buttons on the page you would have to use
List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();
Try this out:
driver.findElements(By.xpath("//button[#class='x-btn-center']").click()
or
driver.findElements(By.xpath("//*[#class='x-btn-center']").click()
Related
I am using the latest chrome driver with selenium to test a web application hosted on Microsoft Azure.
The script starts by logging into the web application. An authentication window opens that requires the user to login through Azure and then press a, "Grant" button that will allow the web application to speak to a Therefore database to populate a few metadata fields.
This all works perfectly when headless mode is disabled. However, when headless mode is enabled it seems as though the, "Grant" button doesn't function. I am logging and taking screenshots during this process, which is how I know that the "Grant" button element is found and is being clicked. The button becomes highlighted when clicked, which is shown in the screenshot, but nothing happens and the authentication window times out in headless which kills the test.
I have tried different clicking methods, but this made no difference as the element is found and is being clicked. I have also pressed the, "Sign in as a different user" button on the form to ensure that the .click() method is functioning as expected, which of course works. I tried to add longer wait times but to no avail.
I have also added the following Chromium driver options:
ChromeOptions options = new ChromeOptions();
options.addArguments("enable-automation");
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
options.addArguments("--incognito");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--allow-insecure-localhost");
options.addArguments("--disable-popup-blocking");
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
How I am clicking the element:
System.out.println("Grant permission...");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Grant')]"))).click();
What baffles me is how this works seamlessly when headless is disabled but not when it's enabled. I'm wondering if this could be a Chrome driver issue? However I know that this is unlikely.
Any recommendations are appreciated, thanks.
Adding button HTML as requested:
<form method="POST">
<p>Hello, Test</p>
<ul>
<li class="text-left">Act with your access permissions</li>
<li class="text-left">Allow continuous access while you are not online.</li>
</ul>
<p>
<button type="submit" name="submit.Grant" value="Grant" class="btn btn-primary btn-block">Grant</button>
<button type="submit" name="submit.Login" value="Sign in as different user" class="btn btn-outline-primary btn-block">Sign in as different user</button>
</p>
Try adding a wait. I mean use WebDriverWait to wait for the element to be clickable.
Something like:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div/div/div/div/div/div[2]/form/p[3]/button[1]"))).click();
Also you need to improve your locator.
Absolute XPaths are extremely fragile.
First, try to avoid very long CSS or Xpath expression. You can find the button you need to click on like this:
driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
This code is more readable. If the site will be changed and the element will be moved to another div or span - your code will not work if it relays on the structure of all the divs and spans.
Next, never just click on an element of hover over it in a Selenium test. First check if the element is clickable, then click:
WebElement term = driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(term));
term.click();
I'm clicking in on a button on a webpage using Selenium. The button creates a file which can be downloaded now. For this, a overlay is shown in Internet Explorer (yes, I HAVE to use this browser, it's a requirement).
Now I have to check the text on the overlay ("öffnen oder speichern" see my screenshot). I can imagine that it there is a solution using JavaScriptExecutor but I simply couldn't found a solution.
I also tried to find it in innerHTML-without success.
It's not an alert so I can't use Driver.switchTo().alert();
My Code still doesn't contain more than clicking on a button using XPath.
Actions action = new Actions(driver);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String exportButtonXPath = generalHelper.getProperty("buttonCSVExportXPath");
WebElement exportButton = driver.findElement(By.xpath(exportButtonXPath));
action.click(exportButton).perform();
Do you have a solution how can test the text on this popup?
Actually, it is not related to the web browser any more. You need to interact with it as a desktop window.
->If you want to click it using selenium, you can locate its coordinates and use click by coordinates using selenium.
->If you want to accept to download it, you can find a capability to accept downloading by default (except IE).
->If you want to check the text value, for sure you've to automate it as desktop not as a web.
I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.
I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();
I've got a button on a webpage that Webdriver will not click when I'm running via IE - I've tried the below workarounds but no luck -
Clicking via Javascript:
((JavascriptExecutor) driver).executeScript("$(arguments[0]).click()", webElement)
Using SendKeys:
webElement.SendKeys(keys.Enter)
Using the Action Builder
Actions test = new Actions(driver);
test.moveToElement(webElement);
test.clickAndHold();
test.release();
test.build();
test.perform();
Making sure the window is the active one, then clicking on the parent object, then the object itself
Problem is, none of them work. I've checked in Firefox and Chrome and the script runs fine. I've confirmed that the element is being found when using IE. Are there any other workarounds I can try?
Seems you are tyring to use JQuery style click... normal javascript style click should work.
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement)
I always found successful with the following for clicking an element in IE.
If is a checkbox/radio: webElement.click();
Clickable input element: webElement.sendKeys("\n");
For other elements, use the above said JS style click.
If the button is a form submit button, you can use : webElement.submit() on another field of the form.