Cant automate Amazon.com signout using Selenium Java - 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();

Related

Clicking a button found with xpath working in Chrome but not in IE11

I want to click a button(to send a form)
<button class="form-button primary">Click here</button>
I find the element like this:
driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;
On chorme is working and sending the form but in IE11 is not(sending the form).
To be clear, in IE is finding the element(or an element). But probably is not the correct element.
Addition info:
This is the only button with this text
I can probably find other ways to get this element , but if I rework this path I will need to change all the paths similar to this.
Selenium version:3.14
IE webdriver :3.14
There are several different ways of clicking on something using selenium. I would try using either a javascript click or an action click.
Javascript click:
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Action click:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();
It's also possible that you are in the wrong frame while you are executing the click.
driver.switchTo.frame("Frame_ID");
You would be able to find the frame ID when you inspect the webpage.
If your usecase is to invoke click() you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath using className and text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='form-button primary' and text()='Click here']"))).click();
xpath using text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Click here']"))).click();
xpath using contains():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(., 'Click here')]"))).click();

Element is clickable when ran in debug mode, but ran normally throws ElementClickInterceptedException

I am trying to do web automation.
I am defining a pop-up menu containing a button defined with either xpath or css respectively as
XPath:-->: //button[contains(text(), 'Open Door')
CSS:-->: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted
While all is well, it throws
org.openqa.selenium.ElementClickInterceptedException: element click intercepted:
when I am debugging the test one step at a time, it
runs successfully by clicking the button, with out any
problem. But when I am running the test, it fails. I hope it is not a wait issue, as we apply check waiting for
the presence of the button and verify it exists and clickable.
I believe many would advice to use JavaScriptExecutor approach, but our framework has a problem of returning any web element as a custom object called "Element" which is neither Web Element nor sub class of it, but extends Object and implements an interface called IElement, so we can't use JavaScriptExecutor method since it needs Web Element form of the button which we want to click on.
If it works in debug it means the overlay disappear automatically. You can wait for it to vanish
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("[id^='device-card']")));
And in any case you can wait for the button to be clickable
button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(), 'Open Door')")));
button.click();
It will look something like:
driver.executeScript("document.querySelector('button.btn').click()")
Just adjust the css
There are a couple of things you need to take care:
XPath //button[contains(text(), 'Open Door'): This xpath can be optimized further in either of the formats:
//button[text()='Open Door']
//button[contains(., 'Open Door')]
CSS: div.device-item.content.view-content > div.detail > div > button.btn.btn-primary.ng-star-inserted looks good however I strongly believe button.btn.btn-primary.ng-star-inserted would also work.
"...Executes successfully in debug mode...": As the browser client gets ample time to settle the webpage dynamics and the desired WebElement gets enough time to get interactive.
"...we amply check waiting for the presence of the button and verify it exists and clickable...": Is a myth as:
presenceOfElementLocated(): Is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
where as:
elementToBeClickable(): Is the expectation for checking an element is visible and enabled such that you can click it.
As your usecase is to invoke click() on the element you need to use the ExpectedConditions as elementToBeClickable().
Example:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_element"))).click();
You can find a detailed discussion in What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver?
Finally, when using the Selenium java clients, of coarse there are methods from JavascriptExecutor but that should be the last resort.
Update
As you are still seeing the error ...ElementClickInterceptedException: element click intercepted..., you can add an additional step to induce WebDriverWait for the invisibility and you can use either of the following options:
invisibilityOf(WebElement element):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOf(overlapping_webelement));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
invisibilityOfElementLocated(By locator):
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("css_overlapping_element")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_clickable_element"))).click();
You can find a relevant detailed discussion in Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
you can move the mouse to that element and then click on it, even I was facing this issue and this solution solved it
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
this happens even when the element is not visible completely onscreen, in this case you can scroll to that element and then use the above code as shown below
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", ele);
Actions clickOnBtn = new Actions(driver);
clickOnBtn .moveToElement("//button[contains(text(), 'Open Door')").click().build().perform;
click element using javascriptExecutor
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
This even happens sometimes with the way we selected the element, the xpath can be changed, we can try to use a parent tag rather than the button and try
you can define your own ExpectedCondition with click action:
public class SuccessfulClick implements ExpectedCondition<Boolean> {
private WebElement element;
public SuccessfulClick(WebElement element) { //WebElement element
this.element = element;
}
#Override
public Boolean apply(WebDriver driver) {
try {
element.click();
return true;
} catch (ElementClickInterceptedException | StaleElementReferenceException | NoSuchElementException e) {
return false;
}
}
}
and then use it:
wait10.until(elementToBeClickable(btn));
wait10.until(new SuccessfulClick(btn));

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.

cannot click on Agree & Continue button in Paypal CheckOut using Selenium

I am using selenium for my testing a retail website . Once I reached the Checkout page , I am selecting the option as Paypal Where a sandbox url is opening.
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07L974777B231831F#/checkout/login
I am able to enter the username and password , clicked on Login button.
After that I am redirected to "Agree & Continue " Page . Where I could not perform any action.
I could see clearly the button properties as below
I have tried the below code, but could not perform any action.
WebElement AgreeandContinue= driver.findElement(By.tagName("input"));
AgreeandContinue.click();
You might have many input elements in the same page. What about trying to select by class or id?
WebElement AgreeandContinue= driver.findElement(By.ByClassName('btn'));
or
WebElement AgreeandContinue= driver.findElement(By.ByClassName('continueButton'));
then using submit() instead of click() as the element is a 'submit' type`
FYI : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html
Looks like the button has an id defined, so that would be the best locator to use: driver.findElement(By.id("confirmButtonTop));
If that doesn't work, then you might have to add some waits for the button to be clickable.
And if that still doesn't work, then it's possible, as it is with many commercial tools, that the button is actually inside a different iframe. Look further up in the html to confirm if this is the case (it'll have a iframe tag). If that's the case, then you'll have to first switch to the iframe before clicking the button: driver.switchTo().frame(...) . How to identify and switch to the frame in selenium webdriver when frame does not have id
If we look at the HTML you have provided the WebElement with value Agree & Continue is within an <input> tag. So we have to construct a unique css or xpath to identify the WebElement as follows:
cssSelector :
WebElement AgreeandContinue= driver.findElement(By.cssSelector("input#confirmButtonTop"));
OR
xpath :
WebElement AgreeandContinue= driver.findElement(By.xpath("//input[#id='confirmButtonTop']"));
Try with ID, but if it is not you can try with other locators , I suggest you have to use xpaths:
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();
also I will suggest that you can use wait until element to be clickable or visible
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//input[contains(#id,'confirmButtonTop')]")).click();
or
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(#id,'confirmButtonTop')]")));
driver.findElement(By.xpath("//*[contains(#id,'confirmButtonTop')]")).click();

Selenium - Java - Unable to click a link

My problem mainly is that my code doesn't run, I tried for more than 2 hours. I have seen many posts also, but some are written in different computer languages (not in Java), so I am confused now.
Below is my code for just clicking a button. All I want to do is click a button and go to new page.
WebDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/Users/Sanya/Desktop/New%20folder%20(2)/page%203%20alerts.htm");
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Try this it works fine for me:
WebElement menuHoverLink = driver.findElement(By.id("your_id"));
actions.moveToElement(menuHoverLink).perform();
You can try the below one...
Actions action = new Actions(driver);
action.click(driver.findElement(By.partialLinkText("Alert"))).build().perform();
It was worked for me :-)
You can use XPath for instance to locate the element on your page:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
Here is more information about how XPath works.

Categories

Resources