Selenium + Java, finding the correct element - java

<button class="positive auth-button" tabindex="2" type="submit"> Send Me a Push </button>
The above is my HTML code.
Code I've tried to find the element:
driver.findElement(By.cssSelector("button[type='submit'][class='positive auth-button']")).click();
and
driver.findElement(By.xpath("button[#type='submit'][class='positive auth-button")).click();

Use the following code for that:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".positive.auth-button[type='submit']")));
button.click();
Hope it helps you!

Try with the following code:
WebElement ele1 = driver.findElement(By.xpath("//button[#class='positive auth-button'][#type='submit']"));
JavascriptExecutor clickbtn = (JavascriptExecutor)driver;
clickbtn.executeScript("arguments[0].click();", ele1);
Also,you can go with following code
new WebDriverWait(driver, 2000).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='positive auth-button'][#type='submit']")));

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".positive.auth-button[type='submit']")));
button.click();

Related

Element Clickable in Java Selenium

I'm trying to click on Calendar but everytime I try to click on the calendar the error pop up as "element click intercepted: Element is not clickable at point (293, 1317)
<input type="text" name="form_fields[travel_comp_date]" id="form-field-travel_comp_date"
class="elementor-field elementor-size-sm elementor-field-textual elementor-date-field
flatpickr-input" placeholder="Date of travel"
pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" readonly="readonly">
Here's my code:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='form_fields[travel_comp_date]']")));
driver.findElement(By.xpath("//input[#name='form_fields[travel_comp_date]']")).click();
Can someone please help me correcting this.
Please try the following code with explicit wait and let me know if it works for you:
enter code here
driver.get("https://www.path2usa.com/travel-companion/");
WebElement dateTravel =
driver.findElement(By.xpath("//input[#id='form-field-
travel_comp_date']"));
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver,
Duration.ofSeconds(5));
js.executeScript("arguments[0].scrollIntoView();", dateTravel);
wait.until(ExpectedConditions.visibilityOf(dateTravel));
if(dateTravel.isDisplayed())
{
js.executeScript("document.getElementById('form-field-
travel_comp_date').value='12/20/2023'");
}
The element you are trying to access is out of the initially presented view port.
In order to click it you first need to scroll the page to bring that element into the visible view port and only after that you will be able to click on it.
Please try this:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Or this
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,600)");
After that try performing your code

Error in finding xpath for a sub-element in HTML

I am using below code
Actions action=new Actions(driver);
action.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement=driver.findElement(By.xpath("//a[#href='https://www.annauniv.edu/iom/home.php']"));
action.moveToElement(oceanManagement).build().perform();
oceanManagement.click();
After hovering the mouse on an element, i am not able to find xpath for sub-element as the sub-element is not being displayed in HTML
Use JavaScriptExecutor to click the element. Try this one,
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
Actions actions = new Actions(webDriver);
actions.moveToElement(facultyOfCivil).build().perform();
WebElement oceanManagement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id="menuItemHilite32"]")));
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView();", oceanManagement);
((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", oceanManagement);

Button not clickable consistently in Selenium Java

I am trying to click a button in selenium java code and it is not clicking all the time. Apparently this is pretty common issue.
I tried below few solutions:
HTML Code :
<button class="btn btn--action btn--border-white btn--my__calculate" style="display: inline-block;">Final Figure</button>
Solution 1:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
if (btnWorkout.isDisplayed() && btnWorkout.isEnabled()) {
btnWorkout.click();
}
Solution 2 :
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement btnWorkout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn--my__calculate")));
btnWorkout.click();
Solution 3:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
None of them worked for me.
Other strange thing is above step passes without an error and button doesn't click as expected
Induce WebDriverWait And Following Xapth.
Try following options.
Option1:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
btnWorkout.click();
Option2: Use Action class
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
Actions action=new Actions(webDriver);
action.moveToElement(btnWorkout).click().build().perform();
Option3: Use JavaScript Executor
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
Curious that the executeScript() option didn't work for you. Keep using your WebDriverWait from option 2, but instead of the click() method, try using Action Chains. I'm not sure how to write this in Java, but it'll be something like this:
Actions action = new Actions(driver);
action.move_to_element(btnWorkout).click(btnWorkout).perform();
Maybe you have another hidden element with same CSS (class). You can try capture the element by linkText or partialLinkText like this:
WebElement btnWorkout = webDriver.findElement(By.linkText("Final Figure"));

Selenium Web Driver Java Element class How?

How to catch an element and sendKeys , becouse my code does not work
Screen
1
driver.findElement(By.xpath("//*[contains(#class, 'ng-valid ng-dirty ng-valid-parse ng-touched')]")).sendKeys("Test")
Use WebdriverWait to identify the element and then use sendKeys.I have provided XPATH as well CssSelector.Hope this helps.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-dirty ng-valid-parse ng-touched']")));
element.sendKeys("Test");
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid-parse")));
element.sendKeys("Test");
If it does not work then check if there any frame avaialable.If does you have to switch to frame first.then you will be able to access the element.
driver.switchTo().frame("frame-name");

How to click on the button as per the HTML provided?

This is the html code for the button in the pop up ( pop up has a lead gen form) -
<button id="getCoupon" class="fetch" data-bind="click: submitForm" type="submit">Fetch Coupon</button>
This is the script which I have written in JAVA in Eclipse. I am able to fill the name, Email and Phone number but I'm not able to click on the button -
driver.findElement(By.id("getCoupon")).click();
As per the comment and URL, you have shared :
You can try with this code :
public class Pawan {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user**\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://vets.cm.qa.preview.vca.webstagesite.com/free-first-exam");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[value='/santa-monica']~div.select-btn"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("fName"))).sendKeys("Pawan");
wait.until(ExpectedConditions.elementToBeClickable(By.id("lName"))).sendKeys("Sharma");
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("ps12#gmail.com");
wait.until(ExpectedConditions.elementToBeClickable(By.id("zip"))).sendKeys("90404");
wait.until(ExpectedConditions.elementToBeClickable(By.id("phone"))).sendKeys("9697989900");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,100)", "");
wait.until(ExpectedConditions.elementToBeClickable(By.id("getCoupon"))).click();
}
}
As per your HTML code, your id is : "getCoupon",
whereas in code you mentioned id as : "getCouponFetch". Please correct and it should work. Code -
driver.findElement(By.id("getCoupon")).click();
If selenium click don't work, use below Java Script click code :
WebElement element = driver.findElement(By.id("getCoupon")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
Firstly, what kind of error did you get??
Try using implicit wait if your getting "NoSuchElementException" like below:
driver.manage().timeOuts().implicitlywait(30,TimeUnit.SECONDS);
Then try using the below way to locate that button:
driver.findElementByXpath("text()[contains(.,'Fetch Coupon')]").click();
As per the HTML you have shared to click() on the button you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='fetch' and #id='getCoupon'][contains(.,'Fetch Coupon')]"))).click();
Update
As an alternative you can use the executeScript() method to invoke the click() as follows:
Using cssSelector:
WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);
Using xpath:
WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='fetch' and #id='getCoupon'][contains(.,'Fetch Coupon')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);

Categories

Resources