Unable to click an element in the webpage - java

I am trying to locate an element and click on that element to go to the other page but the element is not getting clicked but it is not showing any errors or warning. Its just highlighting the element but it is not getting clicked
i am using the following
WebDriverWait wait2 = new WebDriverWait(driver,10);
Thread.sleep(10000);
WebElement elements = driver.findElement(By.id("menuItem_Permissions"));
Actions action = new Actions(driver);
action.moveToElement(elements).click().perform();

Try Using Implicit Wait and a bit of this trick
WebElement element = new WebDriverWait( driver, 10 )
.until( ExpectedConditions.elementToBeClickable
(By.id("menuItem_Permissions")));
WebElement wait = new WebDriverWait(driver,
10).until(ExpectedConditions.visibilityOf(element));
Actions scrollToElement = new Actions(driver);
scrollToElement.moveToElement(wait).perform();
element.click();

In your action string you want to use .build() - so adding it like this.
action.moveToElement(elements).click().build().perform();
And as #Faiz has mentioned, you have declared a WebDriverWait command but its not being called anywhere, not from what you have posted. Avoid the Thread.sleep is where possible. Thats not causing the issue however.
You shouldn't need to use an actions builder for one element, just try using elements.click();

Related

Cant automate Amazon.com signout using Selenium Java

I'm trying to do a simple selenium java automation for automating "sign in and sign out of amazon.com site". I'm able to sign in using element locator techniques, like XPath and CSS selector. But for signout, I'm thrown with ElementNotInteractable exception.
Below is the code that I tried(posting the code segment of signout alone).
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).click();
I have tried the above code segment with different element locator techniques like CSS selector and etc, but no luck.
Kindly suggest if I can find and click the sign-out link in the flyout menu by any other method.
Thanks.
You can try below code in which explicit wait is implemented so it will wait for the element to click
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element1 =driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-
belt']/div[3]/div[1]/a[1]/span[1]"));
element1.click();
ele2=driver.findElement(By.xpath("//a[#id='nav-item-signout']"))
wait.until(ExpectedConditions.elementToBeClickable(ele2));
ele2.click();
Instead of the click() method try this :
WebElement element1 = driver.findElement(By.xpath("//header/div[#id='navbar']/div[#id='nav-belt']/div[3]/div[1]/a[1]/span[1]"));
element1.sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//a[#id='nav-item-signout']")).sendKeys(Keys.RETURN);
And instead of RETURN you can also try ENTER
You can try below code in which mover hover is implemented so it will hover the menu then you can click for sign-out.
WebElement ele = driver.findElement(By.id("nav-link-accountList-nav-line-1"));
Actions action = new Actions(driver);
action.moveToElement(ele).perform();
driver.findElement(By.xpath("//*[#id='nav`enter code here`-item-signout']/span")).click();

Close window button clicked but window is not closed

In selenium I successfully switch to an iFrame which contains a modal window:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='intercom-tour-frame']")))
In this iFrame is a close window button which is clicked "successfully" but the window does not close. By successfully I mean the button is found using the xpath and the action is completed without error in my code.
This is what I'm trying:
#FindBy(xpath = ("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))
private WebElement closeTestTourButton;
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
closeTestTourButton.click();
}
I've also tried:
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
waitForElementAndClick(closeTestTourButton, driver);
return this;
}
The test continues but fails as it tries to do an action but this is not possible due to the still open modal window.
Try clicking the button using the javascript, sometimes the events might not trigger with normal click.
public newCampaignPage clickCloseTestTourButton(WebDriver driver)
{
delay(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", closeTestTourButton);
return this;
}
I would suggest using the WebDriverWait rather delay in your script. Below is the implementation.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));
Possibly you are switching and attempting to click() too early.
To click() on the close window button as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#name='intercom-tour-frame']")));
Induce WebDriverWait for the desired element to be clickable.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/span[1]"))).click();
But as you are using #FindBy presumably you are using PageFactory in PageObjectModel, so you won't be able to invoke WebDriverWait in conjunction with ExpectedConditions directly and you have to create a method. You can find a relevant detailed discussion in How to wait for invisibility of an element through PageFactory using Selenium and Java
Outro
Here you can find a relevant discussion on Ways to deal with #document under iframe
I don't like to answer my own questions but in this case this was the only solution that worked:
Actions builder = new Actions(driver);
builder.moveToElement(closeTestTourButton).build().perform();
builder.sendKeys(Keys.ENTER).perform();
Granted, this is not the most elegant solution but after two days of trying it was the only one that worked.

Getting cannot focus element error for sendKeys function in selenium

Trying to send any data to "from text field" on below link.
https://flights.msn.com/en-in/flight-search
Code is below:
WebDriverWait wait= new WebDriverWait(driver,20);
WebElement dropdown=driver.findElement(By.xpath("(//a[#href='/en-in/weather'])[1]"));
dropdown.click();
driver.findElement(By.xpath("(//a[#href='/en-in/travel'])[1]")).click();
driver.findElement(By.xpath("//li/a[#href='https://flights.msn.com/en-in/flight-search']")).click();
//WebElement from=wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("(//div[#class='place-selector js-place-selector'])[1]")));
WebElement from=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='search']/div[1]/div[3]/div")));
//js.executeScript ("document.getElementById('from').focus();");
//from.click();
Thread.sleep(3000);
//from.sendKeys(Keys.BACK_SPACE);
from.sendKeys("Delhi");
from.sendKeys(Keys.ENTER);
Getting error:
Error:org.openqa.selenium.WebDriverException: unknown error: cannot focus element
you need to send the word Delhi to a element then wait until the list appear next click the li contain it.
WebElement origin = driver.findElement(By.cssSelector("div.js-originplace a"));
Actions actions = new Actions(driver);
actions.moveToElement(origin).click().sendKeys("Delhi");
actions.build().perform();
WebElement from = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul//li[contains(., 'Delhi')]")));
from.click()
I think you have not enter proper xpath for from variable, you have forgot to enter index on last div i have shown below let see it helps if this not work then please print or paste all console error or information
WebElement from=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='search']/div[1]/div[3]/div")));
WebElement from=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='search']/div[1]/div[3]/div[index_no]")));
I think might be because of elementToBeClickable will you just change and let me know change it by presenceOfElement let see if its work or not

What is the best way to scroll to a web element located on the current page within the viewport using Selenium

I am new to the automation and I would like to know how to scroll to a web element on the current page using selenium and java.
I have tries many methods which is described in stackoverflow. But couldn't able to solve my problem.
Solutions I tried:
WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
You can use Actions class provided by Selenium.
public void scrollToElement(WebElement element){
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
}
Here I have added an explicit wait and it will wait until the web element is visible. Maximum waiting time will be 60 seconds. If the web element is not visible within 60 seconds, this will throw an exception. You can increase the waiting time by changing this line.
WebDriverWait wait = new WebDriverWait(driver, 60);
Hope this helps.
To scroll a WebElement on the current page within the Viewport using Selenium and Java you need to induce WebDriverWait for the visibility of element located and you can use the following solution:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_element")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element)
You're passing that element from javascript to java and back to javascript.
That's a bad idea, by the time it makes that round trip it might not be there anymore.
((JavascriptExecutor) driver).executeScript("document.querySelector('#id_of_element').scrollIntoView(true);");
Also, a lot of things that you needed to scroll into view for (with older seleniums), you no longer do.

How to send text to the search field through Selenium Webdriver?

Task:
search FAA in search box :
I have tried this:-
webdriver.select_tabs(search.btnSearch);
Thread.sleep(3000);
WebElement searchbox = driver.findElement(By.id("search-text"));
Actions builder = new Actions(driver);
Actions seriesOfActions = builder.moveToElement(searchbox).click().sendKeys(searchbox, "FAA");
seriesOfActions.perform();
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"search-text\"]")));
element.sendKeys("FAA");
element.sendKeys(Keys.ENTER);
webdriver.enter_key(search.txtSearch, Keys.ENTER);
webdriver.enter_Text(search.txtSearch, "FAA");
webdriver.enter_key(search.txtSearch, Keys.ENTER);
Got this error:-
org.openqa.selenium.ElementNotVisibleException: element not visible
Use below xpath :
(//input[#id='search-text'])[2]
and use like :
driver.findElement(By.xpath("(//input[#id='search-text'])[2]")).sendKeys("FAA");
When you find by this id in console it is giving two elements and first one is not visible but second one is the actual input box.
By definition, Selenium interacts with the browser like a real user would. A real user would not be able to type into a textbox/editbox which is hidden. Either you need to change the visibility of the input, re-evaluate why you need to interact with a hidden element, or use javascript executor to set the value of the input, something like this:
driver.executeScript("arguments[0].value='" + textToEnter + "'", element);
To send a character sequence to the search field on the website https://faatoday.com/ you need to induce WebDriverwait to wait for the search icon to be clickable and then again induce WebDriverWait once again for the desired element to be clickable and then send the character sequence as follows:
Code Block:
driver.get("https://faatoday.com/");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav span.sicon-holder.fabutton#searchicon>i.fa.ssearch.fa-search.fa-lg#sicons"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='navbarNav']//input[#class='search-text form-control mr-sm-2' and #id='search-text']"))).sendKeys("FAA");
Browser Snapshot:

Categories

Resources