Selenium WebDriver Java - Element is not visible - java

I am new to Selenium WebDriver tests and I try to use it at work. I tried many combinations of selectors, xpaths and so on, but I can't move past it. I searched also many similiar topics on stackoverflow, sadly without expected results. What I need is to be able to click on "NO SERVICE" button (a href). When I try, I keep getting error, that this element is not visible. When I try to handle this error using "wait"s, I keep getting another error "Expected condition failed: waiting for visibility of element...". What am I doing wrong?
My code:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("withoutService")));
WebElement x = driver.findElement(By.className("withoutService"));
x.click();
and also a html code snippet from webpage:
<div id="fancybox-outer">
<div id="fancybox-content">
<div style="width:auto;position:relative;">
<div id="serviceReminder" style="width: 765px">
<form id="serviceReminderFrom" method="post">
<div class="homeMessage">
<div class="innerMessage">
<input type="hidden" id="serviceToAddReminderFromAction" name="F_ACTION" value="">
<input type="hidden" id="itemsWithServices" name="itemsWithServices" value="">
<input type="hidden" name="eventTypeName" value="Something">
<div class="ServicesDelivery"><span class="disable-button"></span>
NO SERVICE
ADD SERVICE
<div class="none">
</div>
<div class="clear"></div>
</div>
</div>
</div>
</form>
</div></div></div><a id="fancybox-close" style="display: inline;"></a><div id="fancybox-title" class="" style="display: none;">
</div><span class="fancy-ico" id="fancybox-left-ico"></span><span class="fancy-ico" id="fancybox-right-ico"></span></div>

Your locator By.className("withoutService") can match several elements. You need more specific selector. Try below code:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
WebElement x = waitWait.until(ExpectedConditions.elementToBeClickable(By.linkText("NO SERVICE")));
x.click();

Try the following XPath:
//a[contains(#class, 'withoutService')]
Complete Code:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
WebElement x = waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(#class, 'withoutService')]")));
x.click();
If above code is NOT working, then the element might be inside an iframe. please look my detailed answer here.

Related

Selenium - How to get following sibling?

So I want to target a textbox that appears in a list that comes after the label "First Name" and send a first name to the box, but can't seem to be able to target the textBox...
What I've tried:
WebElement firstNameLocTry = driver.findElement(By.xpath("//label[text()='First Name']/following-sibling::div"));
firstNameLocTry.sendKeys(firstName);
What the li look like:
<li class="WPTO WKVO" role="presentation" data-automation-id="formLabelRequired">
<div class="WBUO WETO WDUO">
<label id="56$551056--uid24-formLabel" data-automation-id="formLabel" for="56$551056--uid24-input">First Name</label>
<div class="WEUO wd-c8594868-6b31-4526-9dda-7d146648964b" aria-hidden="true">First Name</div>
</div>
<div data-automation-id="decorationWrapper" id="56$551056" class="WFUO">
<div class="WICJ">
<div class="WMP2 textInput WLP2 WJ5" data-automation-id="textInput" id="56$551056--uid24" data-metadata-id="56$551056" style="visibility: visible;">
<input type="text" class="gwt-TextBox WEQ2" data-automation-id="textInputBox" tabindex="0" role="textbox" id="56$551056--uid24-input" aria-invalid="false" aria-required="true">
</div>
</div>
</div>
</li>
Any reason my sendKeys just leads to Element not interactable?
The attached HTML code Produce below out put
With the given XPath points to the second "First Name" Div [below pic], when you perform sendKeys, it is obvious that the error "Element not interactable" will be thrown.
Try with below two Xpaths,
1. //label[text()='First Name']//parent::div/following-sibling::div
2. //label[text()='First Name']//parent::div/following-sibling::div//input
Please use following xpath:
//div[text()='First Name']
or try:
//*[contains(text(),'First Name')]

how to deal with reused elements in selenium (duplication)

I am unable to locate element that is a button.
1st Button
<div class="col-md-12 col-sm-12 col-xs-6">
<input type="hidden" value="113" name="vendor_id"/>
<input id="vendor_submit" class="btn btn-primary mb10 SaveBtn" type="submit" value="Save & Close" name="submit"/>
I have used this command to locate it
driver.findElement(By.xpath(".//*[#id='vendor_submit']") ).click();
2nd Button
<div class="col-md-12 col-sm-12 col-xs-6">
<input type="hidden" value="113" name="vendor_id"/>
<input type="hidden" value="" name="vendor_hr_account_id"/>
<input id="vendor_submit" class="btn btn-primary mb10 SaveBtn" type="submit" value="Save" name="submit"/>
Problem
Now as they both are on same page i am unable to locate 2nd button due to duplication factor. Only the difference is type.
1st has value="Save & Close"
2nd has value="Save"
Please help me to locate 2nd button.
If there are two elements with the same id, I would suggest you try using cssSelector with its attribute value which would be unique for both and much faster than xpath as below :-
driver.findElement(By.cssSelector("input#vendor_submit[value = 'Save']")).click();
Edited1 :- If you are getting element is not visible exception when you're going to click, you should wait before click using WebDriverWait until element visible and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement submit = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#vendor_submit[value = 'Save']")));
submit.click();
Edited2 :- If unfortunately above does not work, try to click using JavascriptExecutor as below :-
WebElement el = driver.findElement(By.cssSelector("input#vendor_submit[value = 'Save']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
You can use only the value Attribute in xpath:
driver.findElement(By.xpath(".//input[#value='Save']")).click();
You can differentiate by adding the value condition also in your xpath.
So basically you can use.//*[#id='vendor_submit' and #value='Save'] instead in your driver.findelement
You can use absolute xpath for both the button, as their positions are different in HTML.
For eg : html/body/div[1]/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]
To get this add plugin firebug and firepath in mozilla and get absolute xpath from there by inspect element.

Selenium-Unable to locate element using xpath for div class

I am using selenium webdriver in java.I neead to click a menubar. i tried various xpaths and not working. <div class="menu-toggler sidebar-toggler"> </div> is the element i am trying to click. I used the xpath /html/body/div/div[4]/div[2]/div/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/a.
I am fine with any option that would help me click the menu bar.Stuck here with automation.i use ngwebdriver framework so if it can be done using ngwebdriver is also fine.It would be really great if somebody could help me with this.
<div class="ng-scope" ng-if="loggedIn">
<div class="page-spinner-bar hide" ng-spinner-bar="">
<div class="ng-scope" data-ng-controller="HeaderController">
<div class="page-header md-shadow-z-1-i navbar navbar-fixed-top ng-scope" data-ng-include="'app/main/tpl/header.html'">
<div class="page-header navbar navbar-fixed-top ng-scope">
<div class="page-header-inner">
<div class="page-logo">
<div class="menu-toggler sidebar-toggler"> </div>
</div>
<a class="menu-toggler responsive-toggler" data-target=".navbar-collapse" data-toggle="collapse" href="javascript:;"> </a>
<img class="small-logo" src="assets/img/logo_kart_small.gif">
<div class="top-menu">
</div>
</div>
</div>
</div>
You should try using By.cssSelector() as below:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement sideMenuButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.menu-toggler.sidebar-toggler")));
Actions actions = new Actions(driver);
actions.moveToElement(sideMenuButton).click().perform();
Edited :- If unfortunately above does not work try using JavascriptExecutor to perform click as below :-
WebElement sideMenuButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.menu-toggler.sidebar-toggler")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", sideMenuButton);
Try using firefox's firebug add on that has a built in xpath generator. It's a really accurate generator. Has solved many problems of mine even if I was using a different browser than firefox.

Selenium WebDriver. Select element from div list

I have the following area on the HTML page:
<div class="t2-selector">
<div class="">
USA
<div>
<div>
<div>
<div class="selected" asset-id="129">Google</div>
<div asset-id="130">Microsoft</div>
<div asset-id="126">Apple</div>
</div>
</div>
</div>
</div>
<div class="inactive">
Europe
<div>
<div>
<div>
<div class="inactive" asset-id="127">BT</div>
</div>
</div>
</div>
</div>
<div class="">
Currencies
<div>
<div>
<div>
<div asset-id="135">EUR/USD</div>
<div asset-id="136" class="">GBP/USD</div>
<div asset-id="137" class="">USD/JPY</div>
<div asset-id="138" class="selected">USD/CHF</div>
<div asset-id="139">AUD/USD</div>
<div asset-id="140">USD/CAD</div>
</div>
</div>
</div>
</div>
So I need to select desired element from one of the groups (that should not be inactive, OK).
When I'm selecting a group nothing occurs, no error and I don't see the the selected group opens.
Even for a moment.
But when I'm trying to select an element in previously clicked group I receive
org.openqa.selenium.ElementNotVisibleException: element not visible
error.
So I understand that in the moment I'm clicking on desired element it is not visible since the group not appears open.
But why?
And what can I do to resolve this problem?
Currently I'm using following code:
String selectedGroup = getValue("group",'o');
String xpath1 = "//div[contains(text(),'" + selectedGroup + "')]";
driver.findElement(By.xpath(xpath1)).click();
webElement = driver.findElement(By.xpath(xpath1));
String className = webElement.getAttribute("class");
if(className.contentEquals("inactive"))
throw new ElementInactiveException("Selected group appears inactive. Exiting the test");
String optionAssetID = getValue("assetID",'o');
String xpath2 ="//div[#asset-id='" + optionAssetID + "']";
driver.findElement(By.xpath(xpath2)).click();
the error occur on the following line:
driver.findElement(By.xpath(xpath2)).click();
When clicking on a Group or hovering over it it looks in the following way:
As you can see from the code the selected / opened group receives "group-visible" class parameter.
You can hover over the drop down to open it and then click on your element
// simulate mouse movement to the dropdown
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath(xpath1))).perform();
// wait for the element to be visible before clicking on it
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(xpath2)).click();
You can also try to click using JavascriptExecutor
WebElement element= driver.findElement(By.xpath("Your Xpath"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)

Mousehover does not work - Selenium, Java, Chrome

I am trying to automate the hover on a specified element, in my case is an specified office name.
When I hover on an office the information of it should appear.
My problem is that when I run his code:
String officeId = findElement(designOfficesPosition, num).getAttribute(ConstantsFramework.ID);
WebElement office = getSupport().getDriver().findElement(By.id(officeId));
action.moveToElement(office).build().perform();
getSupport().pause(ConstantsFramework.TIME_OUT_10_SECONDS);
I don't get any errors but I don't see the information of the office. Am I missing something? Any ideas? Thanks
UPDATE
Here you can see a piece of the html:
<div id="officesListPreview">
<div class="roundBox previewOffice officesRotator">
<h3>Office information</h3>
<p class="numbers">
<div id="OPA-AT" class="officeContainer" style="display: none;">
<div id="BPO-BG" class="officeContainer" style="display: block;">
<a class="officeLink" href="http://www.bpo.bg/" target="_blank" style="">
<div class="detailsOffice">
</div>
<div id="BOIP-BX" class="officeContainer" style="display: none;">
SOLVED
What I was missing is that there are two classes Actions and Action. I was using just Action class
This works perfectly!!
WebElement home = driver.findElement(By.xpath(//div[#id='homePage']));
Actions actions = new Actions(driver);
Action mouseOverHome = actions.moveToElement(home).build();
mouseOverHome.perform();
You can also try doing it via HasInputDevices interface.
RemoteWebElement homePage = (RemoteWebElement) driver.findElement(By.xpath(//div[#id='homePage']));
((HasInputDevices)driver).getMouse().mouseMove(homePage.getCoordinates());

Categories