Unable to upload file using sendKeys on input element - java

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");

Related

Trying to click on a hyperlink that includes spaces

I am trying to click on a hyperlink with a space using Java and Selenium. Here is a sample of the code
<h3 class="side menu">
<a class="side-menu" href="/configurations">
<span class="menu-icon ca"></span>Configuration
</a>
</h3>
I have tried using xpath starts-with and contains with no luck.
driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();
To handle dynamic element use WebDriverWait and elementToBeClickable with expected conditions.Use the following Xpath locator strategies.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][contains(.,"Configuration")]')));
element.click()
I think all you need is to add a forward slash:
driver.findElement(By.xpath("//a[starts-with(text(),'/configuration')]")).click();
It works for me in the Chrome console if I type:
$x("//a[starts-with(#href,'/configuration')]")
I would use normalize-space when writing the xpath for the text, which will trim the leading and preceding white chars (spaces/tabs/newlines)
Here is how you should do it in your question.
//a[normalize-space(.)='Configuration']
Tested the xpath in chrome console for click() successfully using javascript.

How to get text/value that doesnt have HTML tags using selenium java

From the below HTML content,
<div id="header_cart"><a href="http://store.demoqa.com/products-page/checkout/" title="Checkout" class="cart_icon">
<em class="count">
1
</em>
<em class="item">item</em>
|
<span class="icon" style="margin-top: 0px;">Cart</span>Checkout<span class="shadow"></span></a>
</div>
I want to read the value from first . I tried with the following command
System.out.println(driver.findElement(By.xpath("//div[#id='header_cart']/a/em[1]")).getText());
I'm getting "item" instead of value "1"
Please help me how can I read the values that doesn't have the html tags.
Try .getAttribute("value"); instead​ of .getText().
To extract the value 1 from the <em> tag you can use :
System.out.println(driver.findElement(By.xpath("//div[#id='header_cart']//em[#class='count']")).getAttribute("innerHTML"));
Update :
Probably, the driver is checking for the text too fast before the browser renders it. So try to induce ExplicitWait i.e. WebDriverWait as follows:
WebDriverWait cart_wait = new WebDriverWait(driver, 10);
cart_wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[#id='header_cart']//em[#class='count']"), "1"));
System.out.println(driver.findElement(By.xpath("//div[#id='header_cart']//em[#class='count']")).getAttribute("innerHTML"));
The simplest way to do this is to use a CSS selector. I think your locator is finding the other <em> for some reason. I've added a wait below in case that is the issue. The text inside the <em> is full of extra spaces so you can add a trim() to clean up the text.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement itemCount = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#header_cart em.count")));
System.out.println(itemCount.getText().trim());

Error as Element is not clickable at point in selenium

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);

Selenium Webdriver - unable to click on button

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();

Selecting a value from <md-select> tag using Selenium Webdriver

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

Categories

Resources