I'm studying Selenium WebDriver and testing my skills on a public site. The issue is that I cannot interact with input element with role='combobox' using Selenium WebDriver + Java.
I need to interact (select 2d option) for the first element of calculator ("Калькулятор"-"Тип расчета" - combobox near this text). Firefox shows the code for it:
<div class="select2-search">
<label class="select2-offscreen" for="s2id_autogen2_search"></label>
<input id="s2id_autogen2_search" class="select2-input" type="text" aria-autocomplete="list" aria-expanded="true" role="combobox" spellcheck="false" autocapitalize="off" autocorrect="off" autocomplete="off" aria-owns="select2-results-2" placeholder="" aria-activedescendant="select2-result-label-17"></input>
When I try to click or sendkeys for it it writes that element (both selects and selects2) is not visible although it is visible and interactable manually. CSS is not transformed. I found that JS can change property to make the element visible, but I tried - and nothing changed, isDisplayed() shows false and I cannot select 2d item of the combobox.
Here is the code (I have left only main part)):
WebDriver driver = new FirefoxDriver();
driver.get("http://sberbank.ru/ru/person/credits/money/consumer_unsecured");
try{
Thread.sleep(1500);
}catch(Exception e)
{System.out.println("Error in Sleep.");}
List<WebElement> selects = driver.findElements(By.cssSelector(".select2-search"));//(".select2-results"));
List<WebElement> selects2 = driver.findElements(By.xpath("//input[#class='select2-input']"));
System.out.println("xpath: "+selects2.size());
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,1000);");
System.out.println("value:"+selects2.get(0).getAttribute("value"));
((JavascriptExecutor) driver).executeScript("arguments[0].style.display='inline';arguments[0].style.visibility='visible';arguments[0].style.height = '20px';arguments[0].style.width = '80px';",selects2.get(0));
System.out.println("visible: "+selects2.get(0).isDisplayed());
System.out.println("enabled: "+selects2.get(0).isEnabled());
System.out.println("height: "+selects2.get(0).getAttribute("height")+",width: "+selects2.get(0).getAttribute("width"));
selects2.get(0).click();
selects2.get(0).sendKeys("По среднемесячному доходу");
It writes:
xpath: 7
value:
visible: false
enabled: true
height: 0,width: 0
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
I also tried to trigger keyevent with JQuery but it didn't help:
((JavascriptExecutor) driver)
.executeScript("var s=window.document.createElement('script');" +
"s.src='D:\\Data\\Selenium\\jquery-1.11.3.min.js';" +
"window.document.head.appendChild(s);");
((JavascriptExecutor)driver).executeScript("arguments[0].focus();var e = jQuery.event.trigger({ type : 'keypress', which : 65 });arguments[0].trigger(e);", selects2.get(0));
It writes "Exception in thread "main" org.openqa.selenium.WebDriverException: arguments[0].trigger is not a function"
Please help me to fix my code to be able to set 2d item of this combobox.
Finally got it working -
driver.get('http://sberbank.ru/ru/person/credits/money/consumer_unsecured')
driver.find_element_by_xpath('//*[#id="s2id_autogen1"]/a').click()
driver.find_element_by_xpath('//ul[#class="select2-results ps-container"]/li[2]').click()
Related
Dom structure :
<li class="slds-dropdown-trigger slds-dropdown-trigger--click slds-m-left--
x-small" data-aura-rendered-by="534:20;a">
<!--render facet: 537:20;a-->
<!--
render facet: 541:20;a--><button class="bare slds-button uiButton
forceHeaderButton oneUserProfileCardTrigger" aria-live="off" type="button"
data-aura-rendered-by="184:190;a" data-aura-class="uiButton
forceHeaderButton
oneUserProfileCardTrigger"><!--render facet: 185:190;a--><!--render facet:
187:190;a-->
<div class="tooltipTrigger tooltip-trigger uiTooltip" aria-describedby="tt-
for-174:190;a" tabindex="-1" data-aura-rendered-by="179:190;a" data-aura-
class="uiTooltip">
<span data-aura-rendered-by="171:190;a" class="uiImage"
data-aura-class="uiImage"><img data-aura-rendered-by="169:190;a"
src="https://c.ap5.content.force.com/profilephoto/005/T/1"
class="profileTrigger" alt="">
</span><span class="tooltip-invisible"
role="tooltip" id="tt-for-174:190;a" data-aura-rendered-by="181:190;a">View
profile</span>
</div></button><!--render facet: 543:20;a-->
</li>
i tried these lines of code for Logout :
First click on Logout symbol:
WebDriverWait wait3 = new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//img[#class = 'profileTrigger']")).click();
JavascriptExecutor jse = (JavascriptExecutor)driver;
/*exe1.executeScript("arguments[0].click();", newbt);*/
jse.executeScript("scroll(250, 0)");
second click on Logout button :
driver.findElement(By.xpath("//a[#class =
'profile-link-label logout uiOutputURL']"));
I am getting error as Element is not clickable at point.
#SrieedherSanthakumar
I have few that would like to clarify first, whether or not are you dependent on wait call. Like you are clicking on something and want to wait for something and then process with your second click ?
If so, you might have to modify your first call something like below :
WebDriverWait wait3 = new WebDriverWait(driver, 20);
driver.findElement(By.xpath("//img[#class = 'profileTrigger']")).click();
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
JavascriptExecutor jse = (JavascriptExecutor)driver;
/*exe1.executeScript("arguments[0].click();", newbt);*/
jse.executeScript("scroll(250, 0)");
once you have waited for your page to be load or expected id is visible then proceed with your second call. I am not able to see whether you are waiting for your object in the first call to load since you are clicking on profileTrigger before moving forward with your second call.
1.Use scrollTo element by selector - this will ensure element is visible:
WebElement element = driver.findElement(By.xpath("//button[#class='oneUserProfileCardTrigger']"));
((JavascriptExecutor) driver)
.executeScript("arguments[0].scrollIntoView(true);", element);
2.Always use delay or wait after scroll before click. It takes time for browser.
3.If nothing helps - use javascript click. Bad solution as it not like real user do, but will work always:
WebElement element = driver.findElement(By.xpath("//button[#class='oneUserProfileCardTrigger']"));
((JavascriptExecutor)driver)
.executeScript("arguments[0].click();", element);
I am trying to write a Java program for my job that logs into Paychex (via Chrome) and downloads reports for me. My code navigates to the website, but I'm stuck at that point. I am using Selenium Chrome web driver, and I am unable to locate the element id for the login field, despite me entering the correct name. I've spent hours trying to figure this out to no avail. I've tried finding the element by id, name, css, and xpath. Nothing has worked! Below is the website url and my code. Any assistance in accessing this element would be beyond appreciated.
Thanks!
WebDriver driver = new ChromeDriver();
driver.get("https://myapps.paychex.com/landing_remote/login.do?TYPE=33554433"
+ "&REALMOID=06-fd3ba6b8-7a2f-1013-ba03-83af2ce30cb3&GUID=&SMAUTHREA"
+ "SON=0&METHOD=GET&SMAGENTNAME=09PZJoiHr8jiAF1z4DL6SopY5OyRzoKSeZ4y"
+ "IhpJe7nkRdeIwtlMrg0rd7X3FRDM&TARGET=-SM-https%3a%2f%2fmyapps%2epa"
+ "ychex%2ecom%2f");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS)
WebElement id = driver.findElement(By.id("USER"));
HTML :
<input name="USER" class="form-control ng-pristine ng-invalid ng-invalid-required" id="USER" required="" type="text" maxlength="50" placeholder="Enter Username" data-ng-model="user.username" data-payx-form-value="siteminder.username" data-ng-change="clearShowError()" data-payx-focus="">
You do have the correct ID but your desired element is in an IFRAME. Switch to the IFRAME first and then you should be able to access your element.
driver.get("https://myapps.paychex.com/");
driver.switchTo().frame("login");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("USER"))).sendKeys("MyUsername");
// do stuff but be sure to switch back to default content if you need to access elements outside the IFRAME
driver.switchTo().defaultContent();
HTML code:
<div class="buttonBG">
<input type="button" onclick="window.location.href='?mod=eA&act=00001';" class="buttonGreen" value="TK">
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen" value="CK">
</div>
Below is my java code, when I try with below code. Can I know whats wrong is in my selenium webdriver code:
driver.findElement(By.xpath("//input[#class='buttonGreen' and contains(#onclick, 'window.location.href='?mod=eA&act=00001')]")).click();
Try to search by value
driver.findElement(By.cssSelector("[value='TK']")).click();
And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its
?mod=eA&act=00001
Edit
Another solution is to insert the buttons to list and click by index:
List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();
You can also try using explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();
This will wait up to 10 seconds for the button to be visible before clicking on it. You can change the selector or time span.
Try using XPath instead of CSS
driver.find_element(By.XPATH, '//input[#onclick=\'window.location.href=\'?mod=eA&act=00001\';\']').click()
Edit
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();
I am using Selenium Webdriver and working on automating an AngularJS Web App on Chrome. It was going pretty well until I hit a dropdown list on the app.
My test keeps crashing everytime I try to select a value from it. I have been doing my research on this and I have only seen 2 solutions (both of which I have tried but don't work)
Use the Select object. This doesn't work because the html tag is not <select>, its <md-select> and this fails the test.
I then tried to just click on the dropdown element and click on the value - driver.findElement(By.xpath("xpath to dropdown list")).click(); and driver.findElement(By.xpath("xpath do dropdown value")).click();
With example 2, I also tried creating it as a WebElement variable and calling click() separate, but this did not work either.
Any ideas on how I can solve this issue?
Update
HTML for the dropdown list
<div ng-switch-when="dropdown" class="ng-scope">
<zf-form-dropdown>
<div class="dropdown">
<div layout="row">
<div flex="50" class="quote-label">
<p ng-bind-html="::label" class="ng-binding">Title</p>
</div>
<div ng-show="false" flex="10" class="tooltip-icon ng-hide" ng-click="showToolTip(field.get('toolTip'))" role="button" tabindex="0" aria-hidden="true"><img src="img/item-question#2x.png"></div>
<md-select flex="" ng-disabled="quote.isRated() || !input.enabled" ng-change="onDropdownChange()" ng-model="input.value" class="ng-valid md-default-theme ng-touched ng-dirty" role="combobox" id="select_0Q9" aria-haspopup="true" aria-expanded="false" aria-labelledby="select_label_0I1" tabindex="0" aria-disabled="false" aria-invalid="false" aria-owns="select_menu_0Q8"><md-select-label class="md-select-label md-placeholder" id="select_label_0I1"><span></span><span class="md-select-icon" aria-hidden="true"></span></md-select-label></md-select>
</div>
</div>
</zf-form-dropdown>
</div>
HTML for the value I want to select
<md-option ng-value="::item.val" ng-selected="item.checked" ng-repeat="item in getOpts()" tabindex="0" class="ng-scope" role="option" aria-selected="false" id="select_option_0QD" value="MR">
<div class="md-text ng-binding">Mr</div>
<div class="md-ripple-container"></div>
</md-option>
The xpath for the dropdown list is //*[#id="select_0Q9"]
The xpath for the dropdown value is //*[#id="select_option_0QD"]
If you are sure that your Xpath is fine then you can also click using javascriptexecutor so try it out like below:-
WebElement element= driver.findElement(By.xpath("//md-option[#id='select_option_0QD']/div[1]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Please feel free to locate the element in above code as per your convenience .
As per me your xpath of dropdown should be like below:-
//md-option[#id='select_option_0QD']
And xpath of first div which I suppose want to click is:-
//md-option[#id='select_option_0QD']/div[1]
Change [1] to [2] if you want 2nd value.
In an another aspect you can also store all your element in the list(As you know you can't use select) and click them as per your need or all.
For that you need to use xpath like:-
//md-option[#id='select_option_0QD']/div
Now implement it into code:-
List<WebElement> allelemts = driver.findElements(By.xpath("//md-option[#id='select_option_0QD']/div"));
for(WebElement ele: allelemts){
driver.findElement(By.xpath("//md-option[#id='select_option_0QD']")).click();
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", ele);
}
Hope it will help you :)
Since you are receiving a NoSuchElementException exception, I believe the issue lies in Selenium not able to identify the element. Try any of the following wait methods and then try to click the element.
Explicit Wait: (Most Preferred)
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("elementID")));
or
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementID")));
Implicit Wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Sleep: (Try to avoid this unless absolutely necessary)
Thread.sleep(1000);
EDIT: Added code to check for element's presence using findElements method.
Before using any of these wait methods you could also check for the presence of your element using findElements method.
List<WebElement> element = driver.findElements(By.id("elementId"));
if (element.size() == 0) {
System.out.println("Element not found");
} else{
System.out.println("Element found");
}
I have finally solved the issue!
I got the Selenium IDE and recorded a session where I got as far as selecting the dropdown menu and selecting my value. I then exported the file as a java test case and was able to read the lines where it selected the values, and they were;
driver.findElement(By.cssSelector("#select_08D > #select_label_005 > span.md-select-icon")).click();
driver.findElement(By.id("select_option_08H")).click();
So first off they both do not use xpath to find the elements, the dropdown menu itself is found with the cssSelector and the value is found by the id.
I am just cross referencing again and the id for the value is select_option_08H but while I am looking at Google Console I can see the id for the value is select_option_189
In order to avoid window dialog of upload file, I'm trying following:
driver.findElement(By.xpath("//span[#class='ab-attachment-item']/input")).sendKeys(filePath);
Following is the HTML code snippet:
<div class="ab-attachments">
<span class="ab-attachment-item" ng-hide="isReadOnly()" style="background-color: transparent;">
<input class="ab-attachment-input ng-isolate-scope firefinder-match" type="file" rx-file-upload="file" accept=".pdf,image/*" style="background-color: transparent;">
But it results into error:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
I want to confirm if this error is because of ng-hide="isReadOnly()" in span tag?
And how this can be resolved using Selenium WebDriver itself(using JavaScriptExecutor or something)?
For handling this dialog other tools like Sikuli, AutoIt can be used; but I want to avoid that overhead.
You should be able to do this:
driver.findElement(By.className("ab-attachment-input ng-isolate-scope firefinder-match")).sendKeys(filePath);
If not try adding an implicit wait before you try and sendKeys
Int timeoutInSeconds = 10;
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("ab-attachment-input ng-isolate-scope firefinder-match"));
//I would use implicit wait and directly call the input element instead
new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[rx-file-upload='file'][accept=".pdf,image/*"]"))).sendKeys("filePathWithExtension");