Selecting a radio button using selenium webdriver java when out of view - java

When I am running the below code run, it only works if I actually manage to scroll to the radio button to get it on the screen in time, otherwise the radio button is not selected.
HTML
<label><input name="GenericID1" type="radio" value="5625"> Sample;| Sat/15/805B</label>
WebDriver
WebDriver driver = new FirefoxDriver();
driver.get("http://samplewebste.com");
WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
oCheckBoxTest.click();
Does anyone have any idea why I actually have to manually scroll to the radio to get it to select actually work, otherwise the command just seems to be ignored without throwing any exceptions?

Try following code to scroll to required element and click on it:
WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
Actions actions = new Actions(driver);
actions.moveToElement(oCheckBoxTest);
actions.click();
actions.perform();
If it not works, try with JavaScript:
WebElement oCheckBoxTest = driver.findElement(By.cssSelector("input[value='5625']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", oCheckBoxTest);
oCheckBoxTest.click()

Related

Selenium: Trying to login but the same page is reloaded after click

I am trying to login a page.
I entered the e-mail & password inputs by element.sendKeys() without any error.
After that I need to click the 'loginButton' button. But the button is defined as non keyboard-focusable.
When I run the automation, the button is clicked. But the automation is not continue with the next page (main page); just reloads the same page with empty inputs.
I tried several ways to click the button and also tried to enter by using 'ENTER' key;
// **1.**
loginButton.click();
// **2.**
robot.keyPress(KeyEvent.VK_ENTER);
// **3.**
Actions act = new Actions(dDriver);
act.moveToElement(driver.findElement(By.id("loginButton"))).click().build().perform();
// **4.**
JavascriptExecutor executor;
All of them seems that I can click the button but after that as I mentioned, the page is reloaded, not continue with the next page.
What else can I try?
A problem I can think about is that you are typing the information too fast and hit the login button right away.
Try to wait before clicking the Login button 0.3 seconds:
Thread.Sleep(300);
Or even a full second:
Thread.sleep(1000);
Tell me if this solves your issue.
Also, the site might be disabled for automation code so add these to your ChromeOptions:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
WebDriver driver = new ChromeDriver(options);
Or
options.addArguments("disable-infobars");
To click() on the element Giriş Yap you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.green_flat#loginButton"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='green_flat' and #id='loginButton'][text()='Giriş Yap']"))).click();

How to click on a button in a Alert?

i have to automate this page using java ,and selenium.
I must click on the link which has label terms and conditions it shows up a box, and then i need to navigate it down, and click on Agree button.
I have already tried:
driver.click("//*[#id=\"field_terms\"]/td[2]/div/div/label[1]/a"); // Click on the link
it opens the box for me, but i stuck after it.
I have 2 problems:
How to scrol down the pop up?
How to click on the Agree button?
Update:
Regarding the scroll problem i used the below method which does not work:
public void scrollDown() {
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("arguments[0].scrollTop = arguments[1];","window.scrollBy(0,450)", "");
}
Try using the CSS Selector to locate the I Agree button
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
It worked in my system. I am not a Java person, this is the code I wrote in python for your reference
driver = Chrome()
driver.get('https://signup.insly.com/signup')
terms_and_conditions = driver.find_element_by_link_text('terms and conditions')
terms_and_conditions.click()
import time
time.sleep(2)
i_agree = driver.find_element_by_css_selector(
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
)
i_agree.click()
Let's start with the scroll: just use JavaScript...
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
Or you can just scroll up to the element needed...
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
For the click 'I agree' use XPATH like this:
"//*[text()='I AGREE']" then just preform click()
Hope this helps you!
Here is the code in Java [Tested & worked].
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://signup.insly.com/signup");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("terms and conditions")).click();
//Click the I AGREE button
WebElement btnIagree = driver
.findElement(By.xpath("//button[#btnclass='primary' and contains(text(),'I agree')]"));
btnIagree.click();
//Verify the check box for T&C is checked
WebElement chkbxTnC=driver.findElement(By.xpath("//*[#id='agree_termsandconditions']/../span[#class='icon-check-empty icon-check']"));
Assert.assertTrue(chkbxTnC.isDisplayed());
The problem is, as soon as you click on the T&C link, it takes few seconds to load the page and since a wait is needed before hitting the I AGREE Button. Apparently, the I AGREE Button is enabled and clickable without the need of scrolling down, so scrolling is not needed here.

Selenium - Unable to click on link from dropdown list

I am trying to click on a link from a dropdown list in the menu. Selenium seems to be able to find the element. But not able to click on it and giving the below exception:
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Cannot click on element
I am using IEDriver to run the code.
Below is the java code I am using to find and click on the element:
File file = new File("C:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("url");
driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/a")).click();
driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a")).click();
Below is the HTML body:
<body><ul class="sf-menu" id="Ul1"><li class = "current"><a target="bodyFrame" href="http://hostname.default.aspx">Home</a><ul></ul></li><li class = "current">Create Usage<ul><li class="current"><a target="bodyFrame" href="../SAMPLEAPPDT/Usage.htm" title="Usage Generator (SAMPLEAPP Rating)"">Usage Generator</a> <ul></ul></li><li class="current"><a target="bodyFrame" href="../NETWORKUG/network_usage/NETWORKUsageUpload.aspx?appId=1" title="NETWORK"">NETWORK</a><ul></ul></li><li class="current"><a target="bodyFrame" href="../NETWORKUG/network_usage/NETWORKUsageUpload.aspx?appId=2" title="RSS Usage Generator"">RSS</a><ul></ul></li></ul></li></ul><iframe name="bodyFrame" id="bodyFrame" src="" width="100%" frameborder="no"></iframe></body>
Please let me know what could be the issue
Assuming you are using some JavaScript code to open/close this dropdown, you might need to wait for the dropdown to open before you can select the element because it is not yet visible. Your second "click" might be too fast after the first one.
For example, you can implicitely wait for a certain
amount of time like so:
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
A better alternative would be to wait for your element to be visible like this:
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a")));
Do this after you click the first "a" element and before you try to click on the second.
Thanks for the responses.
The issue seems to be that after selenium clicks on the menu, the list shows up and disappears again. So the second findElement i was using to click on the link in the menu dropdown was not working since selenium is not able to find the element.
I was able to resolve the issue by making selenium hover over the menu and clicking on the first link in the list
Below is the code I used:
Actions action = new Actions(driver);
WebElement webelement = driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/a"));
action.moveToElement(webelement).moveToElement(driver.findElement(By.xpath("//*[#id=\"Ul1\"]/li[2]/ul/li[1]/a"))).click().build().perform();
Java with Selenium Webdriver: Unable to click On the link list but I am able to print out all the link.
I also tried to click on the link using : linktext, href,JS, xpath, CSS, action.double click and click.
//this is my code
WebElement hometab=driver.findElement(By.xpath("//*[#id='new_nav']/li[1]/a"));
hometab.click();
List<WebElement> homelist1=driver.findElements(By.xpath("//ul/ul/li[1]/ul/li/a"));
int allLinks = homelist1.size();
for(int i=0;i<=allLinks;i++) {
List<WebElement> homelis=driver.findElements(By.xpath("//*[#id='main_form']/div[2]/div/ul/ul/li[1]/ul/li"));
WebElement homelis11=driver.findElement(By.xpath("//ul/ul/li[1]/ul/li[1]"));
System.out.println(homelis.get(i).getText());
WebElement element = homelis.get(i);
System.out.println(homelis.get(i));
System.out.println(homelis.get(i).getText());
homelis11.click();
element.submit();
System.out.println("Inside action class");
Actions actions = new Actions(driver);
actions.moveToElement(homelis11).click().build().perform();
System.out.println("JS click ");
//js click
JavascriptExecutor exec = (JavascriptExecutor) driver;
exec.executeScript("arguments[0].click()", homelis11);
//verify the text on that page
WebElement textq=driver.findElement(By.xpath("//h1"));
System.out.println(textq.getText()+UIActions.tab);
//back to home page with all the menu list
driver.navigate().back();
Thread.sleep(15);
Identify the select HTML element:
WebElement mySelectElement = driver.findElement(By.id("mySelect"));
Select dropdown= new Select(mySelectElement);
or pass it directly to the Select element:
dropdown = new Select(driver.findElement(By.id("mySelect")));
To select an option you can do:
All select/deselect methods will throw NoSuchElementException if no matching option elements are found.
Select by Visible Text (select all options that display text matching the argument):
dropdown.selectByVisibleText("Italy");
or
Select by Index (select the option at the given index. This is done by
examining the “index” attribute of an element, and not merely by counting):
dropdown.selectByIndex(2);
http://loadfocus.com/blog/2016/06/13/how-to-select-a-dropdown-in-selenium-webdriver-using-java/

Webdriver not finding xpath element

I'm trying to write some Selenium tests to test Pandora FMS using the Java implementation of the webdriver exported by the Selenium IDE.
The initial login part works just fine:
driver = new FirefoxDriver();
baseUrl = "http://brmew.lab.brmew.es";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl + "/pandora_console/index.php");
driver.findElement(By.id("nick")).clear();
driver.findElement(By.id("nick")).sendKeys("my");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("credentials");
driver.findElement(By.id("submit-login_button")).click();
Then, the problematic part, which is clicking a menu. I've tried to do the most simple approach:
driver.findElement(By.xpath("//ul[#id='subViews']/li[4]/a/div")).click();
But it did not work, so I tried:
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[#id='subViews']")));
myDynamicElement.click();
You can find the HTML I'm testing this with in this link (it's way too large to paste it here)
Hidden menu
Shown menu
Are you getting any exception? It seems that the element submemu item you are trying to click is invisible. If it get displayed on main menu click or mouse over you need to do that before perform click action to the element. For Example:
//Click main menu to open submenu
driver.findElement(By.xpath(".//*[#id='Views']/div")).click();
//now access submenu
driver.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
Or alternately more preferable way is:
WebElement viewsMenu = driver.findElement(By.xpath(".//*[#id='Views']/div"));
viewsMenu.click();
//or mouse over
Actions action = new Actions(webdriver);
action.moveToElement(viewsMenu).build().perform();
//now access submenu
viewsMenu.findElement(By.xpath(".//ul[#id='subViews']/li[4]/a")).click();
From ur given link, i can't find the monitoring > views > agent detail
But it seems that first u have to click on
monitoring and than wait
than click on
views and wait
than click on ur
agent detail
for mouse hover use the below code
//get element as ur wish by css or xpath or id
WebElement elem = driver.findElement(By.cssSelector("ur locator"));
Actions builder = new Actions(driver);
builder.moveToElement(elem).perform();
makeWait(5);
than after showing the element, than again get next element and use the code for hover.

Java selenium click element not working

I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.
You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();
this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();
Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).

Categories