Getting NoSuchElementException: no such element: Unable to locate element - java

I am trying to automate this from. But when I try to send a text to a text fields it is getting NoSuchElementException. I have tried webdriverwait also. But no luck. There is a popup on top of the window. I closed it and also I tried adding --disable-popup-blocking argument. But still it is not working. So anybody can tell me what I am doing wrong here? I highly appreciate that. Thank you.
System.setProperty("webdriver.chrome.driver", "chromedriver/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
options.addArguments("chrome.switches", "--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,250)");
driver.findElement(By.cssSelector("#txt_insert2055107")).click();
driver.findElement(By.cssSelector("#txt_insert2055107")).sendKeys("Test");
driver.findElement(By.id("txt_insert2055108")).click();
driver.findElement(By.id("txt_insert2055108")).sendKeys("Test");
driver.findElement(By.id("btnsubmit")).click();
driver.close();

You cant send text on your input because you need to switch to iframe firts. Please find below working code:
https://www.frontrush.com/FR_Web_App/Player/PlayerSubmit.aspx?sid=MTA1NTc=-9M6ha/5BuDo=&ptype=recruit
code:
driver.get("https://columbiacougars.com/sb_output.aspx?frform=8&path=mbball");
driver.switchTo().frame(0);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
Actions action = new Actions(driver);
WebElement firstName=wait.until(ExpectedConditions.elementToBeClickable(By.id("txt_insert2055107")));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", firstName);
action.moveToElement(firstName).sendKeys("Test").perform();
WebElement lastName=wait.until(ExpectedConditions.elementToBeClickable(By.id("txt_insert2055108")));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", lastName);
lastName.sendKeys("Test");
action.moveToElement(lastName).sendKeys("Test").perform();
WebElement clickElement=wait.until(ExpectedConditions.elementToBeClickable(By.name("btnsubmit")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clickElement);
driver.close();

Related

How to choose select in Selenium java

I have wicket aplication, and I want to set one select value. Problem is that chrome driver returns error with RESPONSE FindElement ERROR
no such element: Unable to locate element: {"method":"css selector","selector":"#id104"}
(Session info: chrome=94.0.4606.81)
Here is my selenium java code:
System.setProperty("webdriver.chrome.driver", pathToDriver);
System.setProperty("webdriver.chrome.logfile", "C:\\app\\chromedriver.log");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\app\\GoogleChromePortable64\\App\\Chrome-bin\\chrome.exe");
WebDriver driver=new ChromeDriver(chromeOptions);
driver.navigate().to(url);
System.out.println(driver.getCurrentUrl());
WebElement userTextField = driver.findElement(By.id("username"));
userTextField.sendKeys(username);
WebElement passwordTextField = driver.findElement(By.id("password"));
passwordTextField.sendKeys(password);
WebElement okButton =driver. findElement(By.id("OKButton"));
okButton.click();
WebElement crdbTab =driver. findElement(By.linkText("CRDB"));
crdbTab.click();
(new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe_app4"));
WebElement uploadTab = driver.findElement(By.linkText("Upload"));
uploadTab.click();
System.out.println(driver.getCurrentUrl());
WebElement fileUploadButton = driver.findElement(By.id("id5f"));
fileUploadButton.sendKeys(filePath);
driver.findElement(By.id("idb")).click();
driver.findElement(By.className("main")).click();
Select headerDataSelect = new Select(driver.findElement(By.id("id104")));
headerDataSelect.selectByIndex(0);
Code fails on
Select headerDataSelect = new Select(driver.findElement(By.id("id104")));
Can someone give a point on how to solve this issue?
Thanks for help
Try adding a wait there to make elements loaded before accessing them.
Instead of
Select headerDataSelect = new Select(driver.findElement(By.id("id110")));
Try
wait = new WebDriverWait(driver, 30);
Select headerDataSelect = new Select(wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id110"))));
Also make sure the id value id110 is unique and not dynamic here.

Click the Button and get the size of table in Selenium java

I am trying to learn selenium to find elements and I could not clicking the "Edit"buttons and Im trying the get the size of the rows of the table.
Here is my code:
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
TimeUnit.SECONDS.sleep(5);
List<WebElement> we = driver.findElements(By.linkText("Edit")); System.out.println(we.size());
we.get(1).click();
Here is the link that I am working:
https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/
driver = new ChromeDriver();
driver.get("https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
WebElement frame = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("demoFrame"))));
driver.switchTo().frame(frame);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[class=\"dx-link dx-link-edit\"]"))).click();
Use webdriver wait and switchTo frame, you have to switch to iframe first to interacte elements inside the iframe.
once you finish , if you want to interact with elements outside iframe then you have to switch out of it first.
driver.switchTo().defaultContent();
//rest of your code
The element is present inside an iframe and you need to switch iframe first and then you will be able to interact with element.
To overcome synchronization issue you need to induce WebDriverWait() and wait for frameToBeAvailableAndSwitchToIt() and following locator.
To get the link details induce WebDriverWait() and wait for visibilityOfAllElementsLocatedBy() and following locator.
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
new WebDiverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("demoFrame")));
List<WebElement> we =new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Edit")));
System.out.println(we.size());
we.get(1).click();

How many ways can we implement Drag & Drop functionality using Selenium and Java

How many ways can we implement Drag & Drop functionality using Selenium and Java?
Code attempt:
Thread.sleep(3000);
WebElement FROM = driver.findElement(By.xpath("(//div[#class='item-container flex-container-horizontal'])[1]"));
Thread.sleep(3000);
WebElement to = driver.findElement(By.xpath("//div[text()='Product Quality?']"));
Thread.sleep(3000);
Actions act=new Actions(driver);
act.dragAndDrop(FROM, to).build().perform();
Drag and Drop functionality can be implemented in multiple ways as follows:
Using dragAndDrop():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement from = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement to = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).dragAndDrop(from, to).build().perform();
Chaining clickAndHold() and moveToElement():
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement drag = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
WebElement drop = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
new Actions(driver).clickAndHold(from).moveToElement(to).release(from).build().perform();
Video demonstration:

Canceled page load listener because no navigation has been detected

Unable to click a button . Firefox version 56.0
WebDriver wd = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) wd;
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
wd.findElement(By.xpath("//input[#id='sum1']")).sendKeys("5");
wd.findElement(By.xpath("//input[#id='sum2']")).sendKeys("10");
WebElement e1 = wd.findElement(By.xpath("//button[#class='btn btn-
default']"));
js.executeScript("arguments[0].scrollIntoView();",e1 );
Thread.sleep(10000);
e1.click();
I want to click 'Get Total' button but "Canceled page load listener because no navigation has been detected" message is showing.
Induce WebDriverWait and elementToBeClickable() and following xpath
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
WebDriverWait wait = new WebDriverWait(wd, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sum1']"))).sendKeys("5");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sum2']"))).sendKeys("10");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn-default'][text()='Get Total']"))).click();
To click() on the element with text as Get Total you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
WebDriver wd = new FirefoxDriver();
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#sum1"))).sendKeys("5");
wd.findElement(By.cssSelector("input.form-control#sum2")).sendKeys("5");
wd.findElement(By.cssSelector("button.btn.btn-default[onclick^='return total']")).click();
xpath:
WebDriver wd = new FirefoxDriver();
wd.get("https://www.seleniumeasy.com/test/basic-first-form-demo.html");
new WebDriverWait(wd, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and #id='sum1']"))).sendKeys("5");
wd.findElement(By.xpath("//input[#class='form-control' and #id='sum2']")).sendKeys("5");
wd.findElement(By.xpath("//button[#class='btn btn-default' and text()='Get Total']")).click();
Browser Snapshot:

Webdriver fails to click element after a WebDriverWait

First, I was facing an issue that selenium webdriver was not always finding the element and clicking, and I found that WebDriverWait should solve the problem. So, I used this code, for instance:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/div[1]/aside/div/nav/ul/li[3]/a"))).click();
But now I am having a timeout issue, Observing the test to run, I can see that the element is being hover(because it changes the color), but webdriver is not clicking.
Does anyone have tips on how to solve this?
I would recommend refactoring like this, to separate the wait from the click event. Otherwise, its hard to diagnose the cause of your click failure:
WebDriverWait wait = new WebDriverWait(driver, 20);
By locator = By.xpath(".//ul/li[3]/a");
WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(locator))
.ignoring(NoSuchElementException.class);
try {
we.click();
} catch (WebDriverException wde)
{
LOGGER.info("Click failed.", wde);
}
Put the wait and then try JavascriptExecutor to click on your element
WebElement element= driver.findElement(YOUR Locator);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)

Categories

Resources