How to click on Hidden link through Selenium Webdriver - java

I am unable to click on a hidden link through Selenium Webdriver.
I am using the below code:
WebElement dwnld = driver.findElement((By.xpath("////form[#id='aspnetForm']/div[6]/div[2]/div/table/tbody/tr[3]/td[2]/table/tbody/tr[2]/td/a")));
Actions builder = new Actions(driver);
Action hoverAction = builder.click(dwnld).build();
hoverAction.perform();

If you don't want to show the link before clicking on it for some reason you can use javascript to click on it (see JavaScriptExecutor).

String Block1 = driver.findElement(By.id("element ID"));
JavascriptExecutor js1=(JavascriptExecutor)driver;
js1.executeScript("$("+Block1+").css({'display':'block'});");

Related

Cant automate Amazon.com signout using Selenium Java

I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();

Selenium webdriver - Unable to locate element on modal dialog

I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.
I attached a screenshot from the code.
What can you recommend?
Thank you!
By.xpath(".//button[.='/"Submit/"'])
or
By.xpath(".//button[#class='btn btn-default'])
If it found but click doesnt work try that javascript from other comment
I presume you are able to identify the element.However unable to click on that.
Try use following options.
Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();
Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));
Note: If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.
driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.
You could try this:
JavascriptExecutor js= (JavascriptExecutor) driver;
WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
js.executeScript(“arguments[0].click()”, webElement);
Hope it helps.
Try the bellow xpath:
driver.findElement(By.xpath("//div[#class='modal-footer']//button[contains(#class,'btn-default')]")).click();

Clicking on an element in Selenium to bring up File Upload window doesn't work

In facebook navigate to Home and try to click on Add Photos/Videos to bring up the file upload window but it always gives an exception "Element is not clickable at point ..."
I tried the methods given in the first answer of Debugging "Element is not clickable at point" error
but nothing works.
Edit:
Code:
1.
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.click();
2
WebElement element= driver.findElement(By.xpath("//span[text()='Add Photos/Video']"));
element.sendKeys(Keys.RETURN);
3
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
4.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element);
You can use Actions moveToElement
Actions actions = new Actions(driver); // initialize Actions instance
actions.moveToElement(elementToClick).performe(); // scroll the screen to make the button visible
elementToClick.click();
I just tried this and it worked... the File | Open dialog opens.
driver.findElement(By.cssSelector("div._3jk")).click();
Try this:
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);

Selenium - Java - Unable to click a link

My problem mainly is that my code doesn't run, I tried for more than 2 hours. I have seen many posts also, but some are written in different computer languages (not in Java), so I am confused now.
Below is my code for just clicking a button. All I want to do is click a button and go to new page.
WebDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/Users/Sanya/Desktop/New%20folder%20(2)/page%203%20alerts.htm");
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Try this it works fine for me:
WebElement menuHoverLink = driver.findElement(By.id("your_id"));
actions.moveToElement(menuHoverLink).perform();
You can try the below one...
Actions action = new Actions(driver);
action.click(driver.findElement(By.partialLinkText("Alert"))).build().perform();
It was worked for me :-)
You can use XPath for instance to locate the element on your page:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
Here is more information about how XPath works.

handling submenu item with webdriver selenium

I want to click submenu item using selenium webdriver which is invisible bydefault.
It becomes visible on mousehover .
I tried with some code and it is giving error as shown below
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with.
Here the code:
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
//WebElement menuHoverLink = driver.findElement(By.className("current"));
actions.moveToElement(menuHoverLink);
WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Use the Actions class to do a mousehover on your menu item and then a click on the submenu option. You can refer to Actions class to get an overview of the methods available and a good help here to understand how to use these interactions.
Actions actions = new Actions(driver); WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']")).click();
I am hoping your locatros are correct..you might want to use a[contains(#href,'nemc.com/rentals')'
In some applications the Action interactions may not work. Personally i faced the problem and then i used the below solution. I took this solution from selenium issue tracker page.
WebElement targetElement = driver.findElement(By.id("locator"));
JavascriptExecutor js = (JavascriptExecutor) driver;
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
js.executeScript(mouseOverScript, targetElement);
driver.findElement(By.id("Click locator")).click;
Try using the below code. It should work.Try adding perform() to your moveToElement statement as shown below.
Actions actions = new Actions(driver);
WebElement menuHoverLink = driver.findElement(By.linkText("RENT"));
actions.moveToElement(menuHoverLink).perform();
WebElement subLink = driver.findElement(By.cssSelector("a[href='nemc.com/rentals/easy-rent']"));
sublink.click();
I stumbled across a similar issue recently, with phantomJS and ghostdriver. In my case, the problem was the window size - the HTML element was outside the visible area and my mouse movements were having no effect (default size is 400x300, which is rather small).
You can check the window size with
driver.manage().window().getSize()
And you can change it with
driver.manage().window().setSize(new Dimension(width, height));

Categories

Resources