Selenium - Java - Actions Class - Click not working - selenium

When I use the following code for click or double click using Actions class, it seems like mouse click triggers but is not released. Apparently, the button is highlighted, but click doesn't perform completely.
Click Code:
WebElement refreshPreviewButton = driver.findElement(By.xpath("//div[#data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);
// Approach 1
action.click(refreshPreviewButton).build().perform();
// Approach 2
action.movetoElement(refreshPreviewButton).click().build().perform();
Double Click Code:
WebElement refreshPreviewButton = driver.findElement(By.xpath("//div[#data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);
// Approach 1
action.doubleClick(refreshPreviewButton).build().perform();
// Approach 2
action.movetoElement(refreshPreviewButton).doubleClick().build().perform();
Working code of other element on the same page:
WebElement previewInTabButton = driver.findElement(By.xpath("//div[#data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);
action.doubleClick(previewInTabButton);
action.perform();
HTML:
<div class="c3_axnPrvCHR2 c3_rows_nowrap c3_align_center c3_hidden" data-dojo-attach-point="_r2Cont">
<div class="c3_axnPrvCBtns c3_axnPrvCBtnsL c3_rows_nowrap c3_align_center btn-sgmt-grp btn-group c3_br4 c3_bdr" data-dojo-attach-point="_btnsCont1">
<div class="btn-sgmt c3_btn btn-icn-sq btn-icn-sq-34" title="preview" data-dojo-attach-point="_previewBtn">
<svg class="c3i c3i_txt">
<use class="c3i_u" xlink:href="#zi-eye-inactive"></use>
</svg>
</div>
<div class="c3_bdr_r"></div>
<div class="btn-sgmt c3_btn btn-icn-sq btn-icn-sq-34" title="code" data-dojo-attach-point="_codeBtn">
<svg class="c3i c3i_txt">
<use class="c3i_u" xlink:href="#zi-code"></use>
</svg>
</div>
</div>
<div class="c3_axnPrvCBtns c3_axnPrvCBtnsR c3_rows_nowrap c3_align_center c3_gpC16" data-dojo-attach-point="_btnsCont2">
<div class="btn-default-outline2_new btn-sm c3_btn btn-fx" data-dojo-attach-point="_previewInTabBtn">
<svg class="c3i c3i_txt">
<use class="c3i_u" xlink:href="#zi-launch"></use>
</svg>
<div>Preview in tab</div>
</div>
<div class="btn-default-outline2_new btn-sm c3_btn btn-fx" data-dojo-attach-point="_refreshPreviewBtn">
<div>Preview</div>
</div>
</div>
</div>
Edit 1: Added HTML
Edit 2: Added working code of other element on same page

In case clicking web element with Actions click or double click seems like the element was clicked / touched but the click / double click did not actually performed we can add a sleep like Thread.sleep(2000); before applying the Actions click.
Waiting for element clickability or visibility may not be enough here since Actions click works not exactly as Selenium click works. So Actions click may need the element to be in the visible viewport and clickable and also the page should be is a stable state, not during scrolling or rendering process.
So, generally I'd suggest to apply here to actions: wait for element clickability + additional sleep, like the following:
WebElement useContentButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#data-dojo-attach-point='_previewInTabBtn']")));
Thread.sleep(2000);
Actions action = new Actions(driver);
action.doubleClick(useContentButton).build().perform();

For anyone who is having a similar issue:
I had to add wait before performing action.
Thread.sleep(2000);
Actions action = new Actions(driver);
action.doubleClick(useContentButton).build().perform();
I got this trick from hover over each stars and then finally click on 4th star.

Related

How to click on the center of the button using selenium java? Which position of the button is actually getting clicked when button is identified?

I have button in our website, and border areas of the button is not clickable. So, i need to make sure the button is getting clicked at the center. Is it possible via selenium?
I tried using coorinates, but it is not recommended for our scenario.
xpath used : //div/button[#id='clickme']
<div class="col-lg-10 col-sm-12 p-0 pt-4">
<button class="click mb-3 " tabindex="0" id="clickme">+ Click Here</button>
</div>
Java code used to click
WebElement button = driver.findElement(By.id("clickme"));
button.click();
I guess the click is happening sometimes[2 out of 10 times] on the border where it is not clickable(hand symbol not shown) . As a result report says the click action happened, but there is no event fired on the website.
To identify the element with text as Click Here you can use either of the following Locator Strategies:
cssSelector:
"button.click#clickme"
xpath:
"//button[contains(#class, 'click') and #id='clickme'][contains(., 'Click Here')]"

How do I move hover to the following element and then click on the visible button?

This is the element I want my mouse to move on. <div id="div282" class="divAsset">
On Mouse hover, a few elements would be visible and I want to click on
<a class="tileDownloadButton button" title="Download" target="_blank" href="xxxxx"> Download </a>
How do I move hover to the following element and then click on the visible button?
Python
asset = driver.find_element_by_id("div282")
action.move_to_element(asset).perform();
button = driver.find_element_by_xpath("//a[contains(text(),'Download')]");
button.click()
Java
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("div282"))).perform();
driver.findElement(By.xpath("//a[contains(text(),'Download')]")).click();
You can execute jquery to hover on element.
Example in python.
driver.execute_script("$('.classname').trigger('mouseover')") #selecting element based on classname.
Example in Java:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('.classname').trigger('mouseover')");
This will make the elements visible and then you can easily click on the elements.
see other jquery selectors here

Selenium webdriver wait doesn't seem to work in case of overlay

There is an overlay ( a grey color translucent screen ) that comes up when ever one clicks Login button and it stays for few seconds. Because of this, selenium web driver isn't able to find the elements as this overlay kinds of hides them for a while or at least that is what looks to me. How can I handle this? I don't find Thread.sleep to be an efficient way here.
I tried -
public void login(){
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.invisibilityOfElementLocated((By.id("ajax-overlay"))));
wait.until(ExpectedConditions.elementToBeClickable((By.id("okbutton))));
driver.findElement(By.id("username)).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin123");
driver.findElement(By.id("okbutton")).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated((By.id("ajax-overlay"))));
}
but nothing seems to work and I still get error -
org.openqa.selenium.WebDriverException: unknown error: Element <button id="loginDialog:okButton" name="loginDialog:okButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-panel-titlebar-icon synchronous" onclick="PrimeFaces.ab({source:'loginDialog:okButton',process:'loginDialog:okButton loginDialog:username loginDialog:password loginDialog:okButton',update:'startingCashFocus loginDialog:dialogFocus loginDialog:lblPassword loginDialog:lblUsername loginDialog:messages',oncomplete:function(xhr,status,args){handleLoginAttempt(xhr, status, args, loginWidget, null); ;}});return false;" tabindex="1" type="submit" role="button" aria-disabled="false">...</button> is not clickable at point (931, 250). Other element would receive the click: <div id="ajax-overlay" class="ui-blockui ui-widget-overlay ui-helper-hidden eternal" style="display: block;"></div>
Also, there is no way to find out the overlay id and thankfully, selenium gave it in its error details.
Try to click element with one of the following method, which will solve this Exception :
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id('okbutton'))).click().perform();
OR
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.id('okbutton')));

Selenium cannot click menu-item from bootstrap dropdown

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", tmpElement);
List<WebElement> dd_list = driver.findElementsByXPath("//*[#id='masterNavigation']/ul/li[1]/ul/li");
for (WebElement ele : dd_list)
{
System.out.println("Values " + ele.getAttribute("innerHTML"));
if (ele.getAttribute("innerHTML").contains("Event Dashboard")) {
ele.click();
break;
}
}
}
Hi I am trying to Automate bootstrap drop-down menu. It's visibility is hidden by default.Once you hover mouse on it, its visibility property shows visible.I am able to click on drop-down , but after clicking on drop-down my selenium script is not selecting value from drop-down.
Error: Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Cannot click on
element
HTML Code Snippet
<a class="ui-button-text-icons" id="ctl00_Sitemap1_HyperLink1" href="javascript:void(void);">
<span style="padding-right: 1.3em;">Dashboards</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-triangle-1-s"></span>
</a>
<ul style="visibility: hidden;">
<li class="first featureGranted">
Classic Dashboard
</li>
</ul>
Few things
You don't need to traverse though all li elements to find your desire element you can do it with Xpath
I have no Idea why you are using JavaScript to click first Element, but unless click method provided by Selenium is not working I would suggest not to use JavaScript Click
Error suggests that element is not visible, it could be due to multiple reasons. You could wait using Explicit wait until element is visible as mentioned below. it might resolve your issue
Code
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// I have no idea why you are clicking using JavaScript
js.executeScript("arguments[0].click();", tmpElement);
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement eventDashboardMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'Event Dashboard')]")));
eventDashboardMenu.click();

Selenium radiobutton selection

I have an HTML code as follows:
<label class="top" for="id_gender2">
<div id="uniform-id_gender2" class="radio">
<span>
<input id="id_gender2" type="radio" value="2" name="id_gender">
</span>
</div>
Mrs.
</label>
The radio button is getting selected after mouse hover.
I have tried with all possible attributes for selection but I am getting element not found exception.Please let me know how to write java script in webdriver.
Try following code and let me know the result:
Actions action = new Actions(driver);
WebElement hover = driver.findElement(By.xpath("//*[#class='radio hover']"));
action.moveToElement(hover).moveToElement(driver.findElement(By.xpath("//input[#id='id_gender2']"))).click().build().perform();
UPDATE
Actually there is no need in mouse hover. Target radio seem to be initially non-clickable, so you just need extra time to wait for page complete rendering:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#id_gender2")));
driver.findElement(By.cssSelector("input#id_gender2")).click();
Try javascript executor as follow:
WebElement element = driver.findElement(By.id("id_gende‌​r2"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Your element is probably not loaded properly while getting error. You'd better use explicit wait in your script. Wait until your specific element loaded to be visible/clickable. I think, this may help you.
Code snippet:
By yourElementToSelect = By.id("id_gender2");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(yourElementToSelect));
driver.findElement(yourElementToSelect).click();