Wait until element is disabled - java

I use this code to click on a dropdown:
// Wait element to be clickable
WebDriverWait webDriverWait = new WebDriverWait(driver, 10);
webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id(dropDownId)));
// Click dropdown to select item
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement ele = driver.findElement(By.id(dropDownId));
wait.until(ExpectedConditions.elementToBeClickable(ele));
ele.click();
I get error element click intercepted: Element... probably because there is a layer which displayed Login bar. After that the element is disabled and the code throws exception.
Is it possible to implement a listen which waits for the element not to be disabled?

Is this what you are looking for -
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("YourXpath"))).isEnabled();

Related

Selenium test passes on hovering drop-down menu item click but does nothing

The website is https://cloudwise.nl/ I'm trying to click on Dit is Cloudwise > Alle Cludwisers with Selenium using Java. It's a hoverable drop-down menu so I saw people handle this situation with the help of waiting until the presence of Element Located functions. so that's my code piece:
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement menu = wait.until(ExpectedConditions.presenceOfElementLocated((By.xpath("//a[#class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
WebElement submenu = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]")));
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();
But the test still passes and it does not click. What could I be doing wrong?
To Mouse Hover on Dit is Cloudwise and then to click on Alle Cludwisers you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following locator strategies:
Using xpath:
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Dit is Cloudwise']")))).build().perform();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Dit is Cloudwise']//following::ul[1]/li/a"))).click();
Try changing presenceOfElementLocated with visibilityOfElementLocated.
Also, looks like
action.moveToElement(menu).moveToElement(driver.findElement(By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))).click().build().perform();
Can be changed with
action.moveToElement(menu).moveToElement(submenu).click().build().perform();
#Erthan Yumer, you can achieve this in the way your initial implementation with the slight change as given below:
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement menu = wait.until(ExpectedConditions
.presenceOfElementLocated((By.xpath("//a[#class='sf-with-ul'][contains(text(),'Dit is Cloudwise')]"))));
action.moveToElement(menu)
.moveToElement(wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//li[#id='menu-item-6380']//a[contains(text(),'Alle Cloudwisers')]"))))
.click().build().perform();

I can't click on an element

the page is fully loaded and the element is located, but for some reason it can't be clicked, I can't understand why.
My test:
#Test
public void test(){
chromeDriver.get("https://alfabank.ru/currency/");
WebDriverWait wait = new WebDriverWait(chromeDriver, 8);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = chromeDriver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
clicking.click();
}
Full xpatch it doesn't work either:
WebElement clicking = chromeDriver.findElement(By.xpath("/html/body/div[1]/div/div[6]/div/div/div/div/div[1]/div[1]/button[2]"));
Exception:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (539, 1199)
None of the existing answer really explain what is their code is about.
Even if you launch the screen in full screen, USD is not in Selenium view port.
Also JS is only recommended when nothing works.
I am doing with Selenium class, actions, please see below :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://alfabank.ru/currency/");
boolean b = wait.until(ExpectedConditions.titleIs("Курсы валют — «Альфа-Банк»"));
if (b) {
new Actions(driver).moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-test-id='caption']")))).build().perform();
WebElement clicking = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clicking);
}
I use JS click, don't know why cannot click normally.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true); arguments[0].click()", clicking);
Apply scrollIntoView and then click on the USD using JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://alfabank.ru/currency/");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement usdoption = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
js.executeScript("arguments[0].scrollIntoView(true);", usdoption);
js.executeScript("arguments[0].click();", usdoption);

I am unable to extract the correct xpath , can help me

For xpath plaese help me for get xpath
Try //a[#class='registrationBtn']/span[1]
To handle dynamic element induce WebDriverWait and wait for the element elementToBeClickable and then click.
Try following code.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='fr mt-header']/a[#class='registrationBtn' and #title='Client-Patient Registration']")));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='fr mt-header']/a[#class='registrationBtn' and #title='Client-Patient Registration']/span[#class='reg_icon']")));
element.click()

Selenium: ElementNotVisibleException while trying to click the element with text as My Account onhttps://www.phptravels.net/

When I tested phptravel website and tried to click on the myaccount link with the below code.
Selenium returns ElementNotVisibleException during the execution. What is the thing that I missed?
Source code
public void login(WebDriver driver) {
driver.navigate().to("https://www.phptravels.net/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/nav/div/div[1]/a")));
// Error on here
myAccount.click();
WebDriverWait myAccountWait = new WebDriverWait(driver, 10);
myAccountWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id=\"li_myaccount\"]/ul")));
loginLink.click();
WebDriverWait loginWait = new WebDriverWait(driver, 10);
//loginWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\\\"loginfrm\\\"]/div[1]/div[5]/div/div[1]/input")));
username.sendKeys("user#phptravels.com");
password.sendKeys("demouser");
loginBtn.click();
}
myAccount webElement is not initialized in your code.
In case you want to click on My account link you can use this :
WebElement myAccount = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='collapse']/descendant::ul[3]/li[#id='li_myaccount']/a")));
myAccount.click();
Note that you can't use link Text as that is text nodes .
Modify the code as below:
On your WebDriverWait keep the xpath as below with By type:
By myAccountBy = By.xpath("//ul[#class='nav navbar-nav navbar-right']/ul/li[1]/a");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(myAccountBy));
OR
Hardcode the xpath like below.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.Xpath("//ul[#class='nav navbar-nav navbar-right']/ul/li[1]/a")));
Then keep the same xpath for myAccount WebElement as below
#FindBy(xpath="//ul[#class='nav navbar-nav navbar-right']/ul/li[1]/a")
public WebElement myAccount;
In short, to click the MyAccount you have to keep this xpath
//ul[#class='nav navbar-nav navbar-right']/ul/li[1]/a
First you need to create WebElement then use elementToBeClickable Expected Condition for the same this way you can resolve the issue
WebElement myAccount = driver.findElement("Your locator");
Now use wait
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(myAccount));
myAccount.click();
Also maximize the browser.
It is always a best practice to add the explicit wait after loading the URL. I am able to click the Account link based on the below modified XPath.
Xpath: //nav//li[#id='li_myaccount']//a
Working Code:
driver.get("https://www.phptravels.net/");
WebDriverWait wait=new WebDriverWait(driver,10);
//wait is added in order to complete the page loading
wait.until(ExpectedConditions.titleContains("PHPTRAVELS"));
driver.findElement(By.xpath("//nav//li[#id='li_myaccount']//a")).click();
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:

Not able to click on the div tag element

I need to click on the 'Admin' tab.
The html code is as follows
<div id="TransactionLinksTabContainer">
<ul class="TransactionLinksTab listitem-cursor-pointer" id="ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList">
<li><div>Transactions</div></li>
<li><div>Restricted</div></li>
<li><div>Quotes</div></li>
<li><div>Admin</div></li>
</ul>
</div>
I tried this solution which did not work
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//* [#id='TransactionLinksTabContainer']/ul/li[4]/div[text()='Admin']")));
element.click();
Try to click on element using java-script executor method to click on element.
WebElement admin = driver.findElement(By.xpath("//ul[#id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", admin);
OR Use explicit wait method and then try to click on element.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//ul[#id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]"))));
driver.findElement(By.xpath("//ul[#id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]")).click();
You can use descendant function as well for reaching to the required control, as shown below:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#id='TransactionLinksTabContainer']/descendant::div[text()='Admin']")));
element.click();
I would recommand you to use
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='TransactionLinksTabContainer']/ul/li[4]/div[text()='Admin']")));
WebElement element = driver.findelement(By.xpath("//*[#id='TransactionLinksTabContainer']/ul/li[4]/div[text()='Admin']"))
element.click();
I don't think that there is an issue with the XPath, it may be that "Wait element is not able to stored in an Webelement"

Categories