How to click on checkbox with ElementNotInteractableException using Selenium WebDriver Java? - selenium

I would like to click on checkbox but it keep saying that element is not interactable.
I actually tried .click, javascriptexecutor, actions, wait until, thread.sleep, scroll. Even when I try context click on the checkbox it says that element is not interactable.
Any other ideas?
<div class="panel panel-default">
<div class="panel-heading">Single Checkbox Demo</div>
<div class="panel-body">
<p> Clicking on the checkbox will display a success message. Keep an eye on it</p>
<div class="checkbox">
<label>
<input type="checkbox" id="isAgeSelected" value="">Click on this check box</label>
</div>
<div id="txtAge" style="display:none">Success - Check box is checked</div>
</div>
</div>
and here is the url: https://www.seleniumeasy.com/test/basic-checkbox-demo.html
Checkbox code:
#FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView();", checkboxFirstExample);
executor.executeScript("arguments[0].click();", checkboxFirstExample);
return this;
}
Whole code:
public class checkboxDemo extends baseSeleniumClass {
#FindBy(xpath = "//ul[#id='treemenu']//a[contains(text(),'Input Forms')]")
private WebElement inputFormsMenu;
#FindBy(xpath = "//li[#style='display: list-item;']//a[#href='./basic-checkbox-demo.html']")
private WebElement checkboxDemo;
#FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
#FindBy(xpath = "//div[#id='txtAge']")
private WebElement ageSelected;
#FindBy(xpath = "//input[#type='button'][#id='check1']")
private WebElement checkboxAll;
private seleniumHelper helper;
private WebDriver driver;
public checkboxDemo(WebDriver driver){
PageFactory.initElements(driver, this);
this.helper = new seleniumHelper(driver);
this.driver = driver;
}
public checkboxDemo checkboxDemostart(){
checkboxDemo.click();
return this;
}
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView({block: 'center'});", checkboxFirstExample);
checkboxFirstExample.click();
System.out.println("3. " + ageSelected.getText() + " - First checkbox passed");
return this;
}
public checkboxDemo checkboxSecondEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView();", checkboxAll);
executor.executeScript("arguments[0].click();", checkboxAll);
System.out.println("4. " + checkboxAll.getAttribute("value") + " - All checkboxes are selected");
return this;
}
public checkboxDemo inputMenu(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", inputFormsMenu);
return this;
}
}

#FindBy(id = "isAgeSelected")
private WebElement checkboxFirstExample;
public checkboxDemo checkboxFirstEx(){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView({block: \"center\"});", checkboxFirstExample);
checkboxFirstExample.click();
return this;
}
use id , 'isAgeSelected' . You should click the input tag
xpath :
//input[#id="isAgeSelected"]
CSS:
input[id="isAgeSelected"]

Related

No such element in selenium java

public class listenerdemo {
#Test
public void login() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
System.out.println(" The Page Title...." + driver.getTitle());
System.out.println("The Page URL...." + driver.getCurrentUrl());
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(500));
wait.until(ExpectedConditions. visibilityOfAllElementsLocatedBy (By.xpath("//*[#id='webklipper-publisher-widget-container-notification-close-div']")));
driver.close();
}
}
popup

Passing data to local storage

I'm using Cucumber with Java and I'm trying to pass a value to local storage. I'm using also WebDriverManager and when I try to execute some script with JavaScripExecutor, another instance of a browser is opened.
I tried to implement the JavaScriptExecutor in the WebDriverManager but I got the same result as before.
public class WebDriverManager {
private WebDriver driver;
private static DriverType driverType;
private static EnvironmentType environmentType;
private static final String CHROME_DRIVER_PROPERTY = "webdriver.chrome.driver";
private JavascriptExecutor js;
public WebDriverManager() {
driverType = FileReaderManager.getInstance().getConfigReader().getBrowser();
environmentType = FileReaderManager.getInstance().getConfigReader().getEnvironment();
}
public WebDriver getDriver() throws MalformedURLException {
if(driver == null) driver = createDriver();
return driver;
}
public JavascriptExecutor getJavaScriptExecutor() {
js = (JavascriptExecutor) driver;
return js;
}
private WebDriver createDriver() throws MalformedURLException {
switch (environmentType) {
case LOCAL : driver = createLocalDriver();
break;
case REMOTE : driver = createRemoteDriver();
break;
}
return driver;
}
I want to be able to use this executor in the already existing browser instance.
I got some help from a team member and we figured it out already.
public WebDriverManager() throws MalformedURLException {
driverType = FileReaderManager.getInstance().getConfigReader().getBrowser();
environmentType = FileReaderManager.getInstance().getConfigReader().getEnvironment();
driver = createDriver();
}
By passing driver variable to constructor and by creating appropriate constructors in classes I managed to make JavaScriptExecutor use already existing instance of a WebDriver. Example:
JavascriptExecutor js;
public AcceptanceHelper(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, 15);
this.js = (JavascriptExecutor) driver;
}

Why CrossBrowser Automated Script Throws Error in Edge Browser?

Guys i Was Doing Cross Browser Automation in Chrome, FireFox and Edge . I Cant Able To Complete The Test Script in Edge Browser. I Was Getting an Error Message
"org.openqa.selenium.WebDriverException: Unknown error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 42 milliseconds
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'rcktechiess-06', ip: '192.168.1.44', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.edge.EdgeDriver
Capabilities [{applicationCacheEnabled=true, InPrivate=false, pageLoadStrategy=normal, platform=ANY, acceptSslCerts=true, browserVersion=42.17134.1.0, platformVersion=10, locationContextEnabled=true, webStorageEnabled=true, browserName=MicrosoftEdge, takesScreenshot=true, takesElementScreenshot=true, platformName=windows}]
Session ID: B339099D-F7C6-41A7-A6E6-12D22C3D9937
I Have Attched Image For Exception. Please Check it
**Test 1 Code Using Testng**
package Crossbrowser;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Test1 {
WebDriver driver;
#BeforeTest
#Parameters("browser")
public void setup(String browser) throws Exception
{
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox"))
{
//create firefox instance
driver = new FirefoxDriver();
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome"))
{
//set path to chromedriver.exe
System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();
}
//Check if parameter passed as 'Edge'
else if(browser.equalsIgnoreCase("Edge"))
{
//set path to IE.exe
System.setProperty("webdriver.edge.driver","F:\\New folder (2)\\MicrosoftWebDriver (1).exe");
//create Edge instance
driver = new EdgeDriver();
}
else
{
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void testParameterWithXML() throws InterruptedException
{
driver.get("http://xxxxxxxxxx/");
driver.manage().window().maximize();
driver.findElement(By.xpath("/html[1]/body[1]/div[2]/header[1]/ss-header[1]/div[1]/div[1]/div[1]/span[1]/a[1]")).click();
Thread.sleep(2000);
//Find user name
WebElement userName = driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/ss-auth-form[1]/md-card[1]/ss-login-form[1]/div[1]/form[1]/input[1]"));
//Fill user name
userName.sendKeys("koushick#rcktechiees.com");
//Find password
WebElement password = driver.findElement(By.xpath("/html[1]/body[1]/div[2]/div[1]/div[1]/ss-auth-form[1]/md-card[1]/ss-login-form[1]/div[1]/form[1]/input[2]"));
//Fill password
password.sendKeys("1234567890");
Thread.sleep(2000);
driver.findElement(By.xpath("//form[#name='myForm']//ss-submit-button[#label='Log In']//input[#class='submit']")).click();
Thread.sleep(1000);
Test2 obj = PageFactory.initElements(driver, Test2.class);
Thread.sleep(2000);
try
{
Actions a1 = new Actions(driver);
a1.moveToElement(obj.getE1()).click(obj.getE2()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Click on The Elemnet");
}
Thread.sleep(2000);
try
{
Actions a2 = new Actions(driver);
a2.moveToElement(obj.getE3()).click(obj.getE3()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Click");
}
Thread.sleep(2000);
try
{
Actions a3 = new Actions(driver);
a3.moveToElement(obj.getE4()).click(obj.getE5()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Click on The Elemnet");
}
Thread.sleep(2000);
obj.getE6().sendKeys("ss#gmail.com");
Thread.sleep(2000);
obj.getE7().click();
Thread.sleep(2000);
obj.getE8().click();
Thread.sleep(2000);
driver.navigate().to("http://XXXXXXXXXXXX/");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[#classname='nav-link men']")).click();
Thread.sleep(2000);
}
#AfterTest
public void MatchitnowWithXML() throws InterruptedException
{
Test2 obj1 = PageFactory.initElements(driver, Test2.class);
Actions a4 = new Actions(driver);
a4.moveToElement(obj1.getE9()).click(obj1.getE9()).build().perform();
Thread.sleep(2000);
obj1.getE10().click();
Thread.sleep(2000);
obj1.getE11().click();
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollTop = arguments[1];",driver.findElement(By.id("sylnk-it")), 500);
Thread.sleep(2000);
obj1.getE12().click();
Thread.sleep(2000);
Actions a5 = new Actions(driver);
a5.moveToElement(obj1.getE13()).click(obj1.getE13()).build().perform();
Thread.sleep(2000);
try
{
driver.findElement(By.xpath("//button[#class='cal-sly']")).click();
}
catch (Exception e)
{
System.out.println("Not Visible To Click");
}
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
try
{
Actions a6 = new Actions(driver);
a6.moveToElement(obj1.getE14()).click(obj1.getE15()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Move To The Element and Click");
}
Thread.sleep(2000);
try
{
Actions a7 = new Actions(driver);
a7.moveToElement(obj1.getE16()).click(obj1.getE17()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Click on the Element !");
}
Thread.sleep(3000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,250)");
Thread.sleep(1000);
try
{
Actions a8 = new Actions(driver);
a8.moveToElement(obj1.getE18()).click(obj1.getE19()).build().perform();
}
catch(Exception e)
{
System.out.println("Can't Move To The Element and Click");
}
Thread.sleep(2000);
try
{
Actions a9 = new Actions(driver);
a9.moveToElement(obj1.getE20()).click().build().perform();
}
catch(Exception e)
{
System.out.println("Can't Click");
}
Thread.sleep(2000);
try
{
driver.findElement(By.xpath("/html[1]/body[1]/div[2]/ss-app[1]/ss-search-page[1]/div[2]/ss-search-filters[1]/ss-slide-filter[1]/ss-search-filter[1]/div[1]/div[2]/span[1]")).click();
}
catch(Exception e)
{
System.out.println("Can't Click");
}
}
}
Down am Attaching Page ObjectModel Framework Code
**Test2 Code Using POM Framework Using GET Method**
package Crossbrowser;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Test2 {
public WebElement getE1() {
return e1;
}
public WebElement getE2() {
return e2;
}
public WebElement getE3() {
return e3;
}
public WebElement getE4() {
return e4;
}
public WebElement getE5() {
return e5;
}
public WebElement getE6() {
return e6;
}
public WebElement getE7() {
return e7;
}
public WebElement getE8() {
return e8;
}
public WebElement getE9() {
return e9;
}
public WebElement getE10() {
return e10;
}
public WebElement getE11() {
return e11;
}
public WebElement getE12() {
return e12;
}
public WebElement getE13() {
return e13;
}
public WebElement getE14() {
return e14;
}
public WebElement getE15() {
return e15;
}
public WebElement getE16() {
return e16;
}
public WebElement getE17() {
return e17;
}
public WebElement getE18() {
return e18;
}
public WebElement getE19() {
return e19;
}
public WebElement getE20() {
return e20;
}
#FindBy(xpath="/html[1]/body[1]/div[2]/header[1]/ss-header[1]/div[1]/div[1]/ss-user-dropdown[1]/div[1]/ss-svg-icon[1]/span[1]/i[1]")
private WebElement e1;
#FindBy(xpath="/html[1]/body[1]/div[2]/header[1]/ss-header[1]/div[1]/div[1]/ss-user-dropdown[1]/div[1]/div[1]/a[9]")
private WebElement e2;
#FindBy(xpath="//button[#id='ranomizeButton']")
private WebElement e3;
#FindBy(xpath="/html/body/div[2]/header/ss-header/div/div[2]/ss-user-dropdown/div")
private WebElement e4;
#FindBy(xpath="/html/body/div[2]/header/ss-header/div/div[2]/ss-user-dropdown/div/div/a[3]")
private WebElement e5;
#FindBy(xpath="/html[1]/body[1]/div[2]/div[4]/div[2]/div[1]/div[2]/div[2]/div[1]/div[1]/input[1]")
private WebElement e6;
#FindBy(xpath="/html[1]/body[1]/div[2]/div[4]/div[2]/div[1]/div[2]/div[2]/div[1]/button[1]")
private WebElement e7;
#FindBy(xpath="/html[1]/body[1]/div[2]/div[5]/div[1]/div[1]/div[2]/div[1]/div[1]/button[1]")
private WebElement e8;
#FindBy(xpath="/html/body/div[2]/ss-app/ss-search-page/div[4]/div[2]/ss-products-list/div[2]/span[3]/ss-product-cell/div/div[4]/span")
private WebElement e9;
#FindBy(xpath="/html/body/div[2]/ss-app/div[6]/div/ss-auth-form/md-card/div[1]/span[1]/div/div[2]/label/span")
private WebElement e10;
#FindBy(id="C5")
private WebElement e11;
#FindBy(id="slynk_save")
private WebElement e12;
#FindBy(xpath="/html[1]/body[1]/div[2]/ss-app[1]/ss-header[1]/div[1]/div[1]/span[2]")
private WebElement e13;
#FindBy(xpath="/html[1]/body[1]/div[2]/ss-app[1]/ss-search-page[1]/div[4]/div[2]/ss-products-list[1]/div[2]/span[1]/ss-product-cell[1]/div[1]/div[5]")
private WebElement e14;
#FindBy(xpath="/html[1]/body[1]/div[2]/ss-app[1]/ss-search-page[1]/div[4]/div[2]/ss-products-list[1]/div[2]/span[1]/ss-product-cell[1]/div[1]/div[5]/a[2]/span[1]/i[1]")
private WebElement e15;
#FindBy(xpath="//ss-product-cell[#id='110743~Dresslily~womens-tops~womens-tops']//div[#class='product-cell-container extended-cell']//div[#class='bottom-icons']")
private WebElement e16;
#FindBy(xpath="//span[#id='~110743~Stylish Lace Embellished Short Sleeve Scoop Neck Womens TShirt~Dresslily~Dresslily~Dresslily~womens-tops~womens-tops~~undefined~$4.99~$9.67~~^^womens-tops^womens-clothes^women ']//i[#class='icon-12']")
private WebElement e17;
#FindBy(xpath="//ss-product-cell[#id='304028~GuessFactory~shorts~shorts']//div[#class='product-cell-container extended-cell']//div[#class='bottom-icons']")
private WebElement e18;
#FindBy(xpath="//span[#id='~304028~Shelby Denim Shorts~GuessFactory~Guess Factory~Guess Factory~shorts~shorts~~undefined~~$44.99~~^^shorts^womens-clothes^women ']//i[#class='icon-12']")
private WebElement e19;
#FindBy(xpath="/html[1]/body[1]/div[2]/ss-app[1]/ss-search-page[1]/div[2]/ss-search-filters[1]/ss-slide-filter[1]/ss-search-filter[1]/span[1]")
private WebElement e20;
PS: While Executing The Test Script are Sucessfully Passing in Chrome and FireFox. But Failing in Edge Only at The Middle of Automation Some Kind of Stack Error. Down Am Also Attching The XML Please Give Me a Solution To Find a BetterWay.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" >
<test name="FirefoxTest">
<parameter name="browser" value="firefox">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
</test> <!-- Test -->
<test name="chromeTest">
<parameter name="browser" value="chrome">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
</test> <!-- Test -->
<test name="EdgeTest">
<parameter name="browser" value="Edge">
<classes>
<class name="Crossbrowser.Test1"/>
</classes>
</parameter>
</test> <!-- Test -->
</suite> <!-- Suite -->

New Tab is not opening in Selenium

New Tab is not opening in Selenium
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element=driver.findElement(By.linkText("Gmail"));
Thread.sleep(3000);
element.sendKeys(Keys.CONTROL,"t");
}
}
Please help me find the error
Here is working code..it opens Gmail into new tab and switch to it.
import org.testng.annotations.Test;
public class SimpleTest {
#Test
public void TestMethod() throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Gmail")).sendKeys(selectLinkOpeninNewTab);
Thread.sleep(3000);
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
}
To open a New Tab with the linkText("Gmail") you can use the following code block :
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
open the link in new Tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("http://www.google.com/")).sendKeys(selectLinkOpeninNewTab);
You can use JavaScriptExecutor to do it
Javascriptexecutor js = (Javascriptexecutor)driver;
js.executescript("var win = window.open('"+ YourURLHere + "', '_blank');win.focus(); ");
Above code will open new tab and navigate to the URL provided and focus on the new tab.

Is the fluent webdriver initialized right?

This is kinda follow-up for my question Test causing error occasionally. I initialize my driver
public class TestSuite {
public static WebDriver driver;
#BeforeClass
public static void setUpClass() {
driver = new FirefoxDriver();
}
public class FluentDriver extends TestSuite {
public static WebElement fluentWait(final By locator) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(300, TimeUnit.SECONDS)
.pollingEvery(50, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(
new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return element;
}
;
}
But this this not handling the script which certainly is on the page, but I can't access the source of it.
FluentDriver.fluentWait(By.id("id")).click();
FluentDriver.fluentWait(By.xpath("//a[starts-with(#href,'/problematic_url.html')]")).click();
FluentDriver.fluentWait(By.className("green_true")).click();
Clicking the "id" pulls out a submenu, where the problematic url is. On webpage source (Ctrl+U) the url is present the whole time.
I got this working by adding Javascript enabling Firefox profile on driver. Then I use the FluentDriver solution. It works most of the time.