Selenium click on javascript href - java

I'm facing an issue writing a Selenium program using Java.
I'm trying to click the following link :
חפש
by using the following code:
WebDriver driver = new HtmlUnitDriver();
driver.get("http://mapi.gov.il/Pages/LotAddressLocator.aspx");
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
WebElement element = driver.findElement(By.id("AddressInput"));
element.sendKeys("הנגיד 16");
WebElement button = driver.findElement(By.id("helkaSubmit"));
button.click();
String pageSource=driver.getPageSource();
System.out.println(pageSource);
But it doesn't seems like the button was clicked at all, since the page content which must change after clicking the link, does not change at all.
I need to find a way to check whether the link was clicked at all, or to find why it was not clicked.
Can anyone point me to the solution ?
EDIT:
Using executor.executeScript("document.getElementById('helkaSubmit').href='http://www.google.co.il';");
executor.executeScript("document.getElementById('helkaSubmit').click();");
Worked just fine, so javascript is enabled and clicking is executed, but for some reason the original code does nothing.

try this,
driver.findElement(By.id("helkaSubmit")).click();

Related

How to check the text on an overlay using Selenium?

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.

JAVA/Selenium - clicking the first button on the page works 50% of the time

I'm using Selenium to automate some UI clicking for a web app. One one of the pages I have several buttons leading to some details. They all have the same name, but I'm ok with clicking either one, so I'm just clicking the first one. But... sometimes it works and sometimes it doesn't.
WebElement DetailsButton = setPresentElementByXpath("//input[1][#type='button' and #value='Go to Details']");
DetailsButton.click();
I'm using setPresentElementByXpath to dynamically wait for the element.
private WebElement setPresentElementByXpath(String xpath) throws Exception {
WebElement myDynamicElement = (new WebDriverWait(driver, 15))
.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
return myDynamicElement;
}
What am I doing wrong?
EDIT: I forgot to mention where it fails. It goes through DetailsButton.click(); without issues, but then it fails on clicking the next thing, the screenshots and logs say that the page that was supposed to be displayed after clicking the button was not there, so I'm assuming the button is not clicked.
Page load speed variation and caching might be the issue. If the element not found exception is happening, use an Explicit Wait for it to be clickable via ExpectedConditions.ElementToBeClickable(By).
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.id("myDynamicElement")));
If the exception "another element would receive the click" is happening you can click the element directly with the JavaScriptExecutor. Note: The JS action has no return value (if it worked or not) and does not wait for a page load if one happens after the click, like the Selenium .click() does.
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click()", element);

Issue with focus in ie browser using selenium webdriver code

Firstly i need to mouse over and then it opens a drop down then i will click on that link.Its working fine in firefox,chrome and issue is in ie.
Here is the code
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act1=new Actions(driver);
//act1.click(element2);
act1.moveToElement(element2).click(element2).build().perform();
It works even in IE browser, when we use this piece of code
caps.setCapability("requireWindowFocus", true);
If we use requiredwindow focus its working fine even in IE browser so no issues with locators
But am not encouraged to use above requiredwindowfoucs code in my project.
Is there any other way to do it.
The issue in Ie browser when we are not using requiredwindowfocus, it is clicking on some other link, so am assuming the problem is with focus.
so kindly help me with this issue without using requiredfoucswindow
I have has similar issues in my project. In IE, clicking drop down values does not work. I have a pretty weird and unexpected solution for this. Find the element before performing the operations. I am assuming that this is required because when you move to element1 and when trying to find element the IE Xpath processor messes up the focus.
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
act.moveToElement(element2).click(element2).build().perform();
Please try this and let me know if this works for you too!!
Also, consider using relative XPaths instead of the absolute XPaths you have used. The second Action is not required. One action should do the trick.
EDIT Based on user comment
Move to element1 is working and the list is getting displayed. So I am assuming that element2 is visible and so we can directly click on it without using the Actions class
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
element2.click();

How can i select text from browser screen with help of mouse events in selenium?

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();

Workarounds for Selenium not clicking button with InternetExplorerDriver

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.

Categories

Resources