I saw a lot of threads with this issue but none is working for me as I tried almost all possible methods and still get error "Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted"
System.setProperty("webdriver.chrome.driver", "C://Driver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]")).sendKeys("568567546754");
driver.findElement(By.xpath("//form[#role='search']/div[2]/div[1]/div[3]//center/input[1]")).click();
//driver.findElement(By.xpath("//div[#class='RNNXgb']/div/div[2]/input")).sendKeys("3462355452354");
//Parent-child relationship xpath - Define xpath for parent
//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]
//driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]"));
//driver.findElement(By.xpath("//div[#class='FPdoLc tfB0Bf']/center/input[1]")).click();
//driver.findElement(By.xpath("//iframe[contains(#src, 'consent.google.com')]")).click();;
//Thread.sleep(2000);
//driver.findElement(By.xpath("//*[#id='introAgreeButton']/span/span")).click();
//driver.findElement(By.linkText("Google Search")).click();
//driver.findElement(By.id("lst-ib")).sendKeys();
//driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
//driver.findElement(By.name("btnK")).click();
//driver.manage().window().maximize();
Please try this, it will work. As Enter will do the same thing as clicking on the Search button.
driver.findElement(By.name("q")).sendKeys("568567546754" + Keys.ENTER);
Here I'm not using the xpath for search text field as I've found name attribute.
You can send the search text and Enter in one shot.
You can use Action class here.
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[#title='Search']")).sendKeys("568567546754");
Robot ab = new Robot();
ab.keyPress(KeyEvent.VK_ENTER);
You can use this reduced Xpath : //input[#name='q'] or driver.findElement(By.name("q")).sendKeys("568567546754");
or you can try this
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("\$x("//input[#name='q']")[0].value="Car"\");
check out this code snippet
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class SearchAction{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
// identify element
WebElement p=driver.findElement(By.name("q"));
//enter text with sendKeys() then apply submit()
p.sendKeys("Selenium Java");
// Explicit wait condition for search results
WebDriverWait w = new WebDriverWait(driver, 5);
w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul")));
p.submit();
driver.close();
}
}
Related
Code trials:
import java.util.List;
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.interactions.Actions;
import org.testng.annotations.Test;
public class DragDrop {
WebDriver driver;
#Test
public void dragdrop() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
//Bring the element to focus
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0,600)");
Actions act = new Actions(driver);
Thread.sleep(5000);
//driver.findElement(By.xpath("//span[text()='Nancy Atherton']//parent::div//parent::li"));
//The below web element is where the issue is and unable to locate the web element
WebElement allBooks = driver.findElement(By.xpath("//span[text()='Nancy Atherton']"));
WebElement myLib = driver.findElement(By.xpath("//ul[#role='listbox']"));
act.dragAndDrop(allBooks, myLib).perform();
}
}
Error Message:
FAILED: dragdrop
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[text()='Nancy Atherton']"}
(Session info: chrome=109.0.5414.75)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
I am unable to locate the element even after trying different locators like xpath, css, etc.
I have also noticed that there are iframes tag present up the hierarchy. I even tried by identifying and moving to that particular iframe and then locating the element but it didn't help.
I would really appreciate if anyone can take a look at the program and tell where am i going wrong?
The desired elements are within a iframe so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use the following Locator Strategies:
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='//dhtmlxcode.com']")));
WebElement books = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='dhx_tree-label' and text()='All Books']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("return arguments[0].scrollIntoView(true)", books);
WebElement dragMe = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='dhx_tree-list-item__text' and text()='Nancy Atherton']")));
WebElement dropHere = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#treeTarget>ul")));
Actions actions = new Actions(driver);
actions.dragAndDrop(dragMe, dropHere).build().perform();
public class DragDrop {
WebDriver driver;
#Test(description="This is drag and drop scenario..")
public void dragdrop() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0,500)");
WebElement frame_ele = driver.findElement(By.cssSelector("iframe[src*='//dhtmlxcode.com']"));
driver.switchTo().frame(frame_ele);
WebElement allBooks = driver.findElement(By.xpath("//span[text()='Nancy Atherton']"));
WebElement myLib = driver.findElement(By.xpath("//ul[#role='listbox']"));
Actions act = new Actions(driver);
act.dragAndDrop(allBooks, myLib).perform();
Thread.sleep(3000);
driver.quit();
}
}
What is the wrong in my code , why auto click doesn't work in accept cookies button. This website using angular application.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
public class NewClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
NgWebDriver ngWebDriver = new NgWebDriver(driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
ngWebDriver = new NgWebDriver((JavascriptExecutor) driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
}
}
Attached Image here
When you use this line
driver.findElement(ByAngular.options("onetrust-accept-btn-handler")).click();
it will try to find the element immediately, often resulting in errors. Cause web element/elements have not been rendered properly.
This is main reason we should opt for Explicit waits, implemented by WebDriverWait.
They allow your code to halt program execution, or freeze the thread,
until the condition you pass it resolves. The condition is called with
a certain frequency until the timeout of the wait is elapsed. This
means that for as long as the condition returns a falsy value, it will
keep trying and waiting.
Code :
Webdriver driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
As per my knowledge there is no way to auto accept cookies using ChromeOptions you need to find the element and click.
driver = new ChromeDriver();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login/");
new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("onetrust-accept-btn-handler"))).click();
Please add wait functionality before entering username and password,
if pop up interrupt while entering, username and password will not be proper.
In my case, i created a common method wait and click,
dh.wait_for_the_element_then_do(By.xpath("//button[text()='Accept All Cookies']"), "click", "", "cookies popo up");
dh.wait_for_the_element_then_do(By.xpath("//input[#tcp-auto='input-username']"),"send", username, "login username");
dh.wait_for_the_element_then_do(By.xpath("//input[#tcp-auto='input-password']"),"send", password, "login password");
dh.wait_for_the_element_then_do(By.xpath("//span[text()='Sign in']"),"click", "", "signin button");
This is the solution of my problem.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.paulhammant.ngwebdriver.ByAngular;
import com.paulhammant.ngwebdriver.NgWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
public class NewClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Hp\\Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
NgWebDriver ngWebDriver = new NgWebDriver(driver);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://visa.vfsglobal.com/ind/en/deu/login");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions
.elementToBeClickable(By.id("onetrust-accept-btn-handler")))
.click();
I tried the following code to enter the value BLR in a auto suggestive dropdown however although its clicking it, its now entering the text.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class testcase2 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "//Users//suva//Downloads//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
WebElement source = driver.findElement(By.id("fromCity"));
source.click();
System.out.println(source.isEnabled());
Thread.sleep(2000);
source.sendKeys("BLR");
//source.sendKeys(Keys.ARROW_DOWN);
}
}
After you click on the default 'from' selection, there is an dropdown with another input to type.
Try like this:
driver.get("https://www.makemytrip.com/");
WebElement triggerFromDropdown = driver.findElement(By.id("fromCity"));
triggerFromDropdown.click();
WebElement fromInput = driver.findElement(By.css(".autoSuggestPlugin input[placeholder='From']"));
fromInput.sendkeys('Dubai');
There can be many reasons why it is not working. It would ave been beneficial if you could provide the DOM element as well..
However a solution would be to enter text through JavaScript Executor.
The code will be something like :-
WebElement webelement = driver.FindElement(By.id("fromCity"));
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].value='" + "BLR" + "';", webelement);
For better authenticity of the above code, I need DOM. It should work though.
I am writing the following code to click on the element with text as My Account. It is showing "element not visible". To resolve the issue I am trying expected wait, but it is being timed out. Is there any way around. You can find my code below:
package com.php.travels;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LogIn {
public static void main(String[] args) {
System.setProperty(
"webdriver.chrome.driver",
"/Users/owner/desktop/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.phptravels.net");
try {
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//li[#id ='li_myaccount']/a"))
).click();
}
catch(Throwable t){
System.out.println("The execption is: " + t);
}
finally {
System.out.println("If no exception tell me now");
}
}
} // end class
To click() on the element with text as My Account instead of visibilityOfElementLocated() you need to induce WebDriverWait through elementToBeClickable() method and you can use the following solution:
Code Block:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.phptravels.net");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//nav[#class='navbar navbar-default']//li[#id='li_myaccount']/a[normalize-space()='My Account']"))).click();
Browser Snapshot:
driver.get("https://www.facebook.com/sachin.aryal");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
driver.findElement(By.xpath("//*[#id='u_0_1a']/div/div[6]/div/ul/li[2]/button")).click();
The last line of the code is not working. How do I set the XPath of the Post button on facebook?
I know you already marked your own answer but it's not the proper way to do this. There are ways built into Selenium to do waits, e.g. wait for an element to be clickable. This method is the proper and more efficient way to do this.
driver.get("https://www.facebook.com/sachin-aryal/");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[.=\"Post\"]"))).click();
I noticed it works when you first scroll into view the button, try adding before the click on post the:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)","");
Try the below code .. it must work for you..
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Facebook_login {
public static void main(String[] args) throws InterruptedException {
String user_name = "facebook_user_name";
String pwd = "facebook_password";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys(user_name);
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(pwd);
driver.findElement(By.xpath("//input[contains(#value,'Log In')]")).click();
Thread.sleep(10000);
System.out.println("logged in successfully");
WebElement notification = driver.findElement(By.xpath("//a[contains(#action,'cancel')]"));
if(notification.isDisplayed()) {
System.out.println("Notification is present");
notification.click();
}
WebElement status =driver.findElement(By.xpath("//textarea[#name='xhpc_message']"));
status.sendKeys("Hello");
Thread.sleep(3000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();
}
}