I ran into a little issue today while validating some input fields in our application.
Looks like we can't call the selenium java method clear() to clear the input and I know I can iterate over the string and use sendKeys(Keys.Backspace) but clear method should do the work?
Anyone else ran into the same issue and how did you work around this problem.?
Our app is built upon Ant libraries
Selenium
String hotmarketNameEdit = "ant-input";
WebElement edit = driver.findElement(By.className(hotmarketNameEdit));
edit.click();
edit.clear();
HTML
<input type="text" id="label" class="ant-input ant-input-sm" value="AUTOMATION TEST - Thu Oct 21 09:54:15 MDT 2021" xpath="1">
Thanks for any inputs
I've personally come across this issue quite a few times, that Selenium .clear() method does not work properly.
Selenium community is aware of this issue, please see the bug reported here - ClearText does not work
You can go through the above-mentioned link.
Workaround :
Majority of time, I try to simulate Ctrl + A to select the text and just press delete after that.
If You are using Java as a binding language, You can do the following
webElement.sendKeys(Keys.CONTROL + "a")
webElement.sendKeys(Keys.DELETE)
have you tried JavaScriptExecutor?
JavascriptExecutor jse = (JavascriptExecutor) indexWebDriver;
jse.executeScript("arguments[0].value=''",edit);
check this one also
How can I consistently remove the default text from an input element with Selenium?
Can you please try this work around, passing both selecting and deleting keywords in one command
driver.findElement(By.name("username")).sendKeys(Keys.chord(Keys.CONTROL,"a", Keys.DELETE));
Related
I'm new in Selenium and Java, i'm work in a Automation testing project using selenium (java) , today i have a problem with driver.findElement, the element i want to find was created by javascript, i'm sure about using WebDriverWait to wait to target element to present, even i use thread.sleep and wait for it for about few minutes but webdriver still can not locate that element.
This is my problem, in html/js code i have a button, click this button javascript will create a div and i need use selenium to locate it to make Automatio Testing work.
After click a button, javascript makes a div look like :
<div id="zm-view-frame" class="fade in"><div class="zm-view-header"><div class="zm-view-back"><a onclick="WFLandingpage.closePreview();" id="zm-view-close" class="button" href="javascript:void(0);"><span>Close</span></a></div><div id="zm-view-button"><span>Desktop</span><span>Tablet</span><span>Mobile</span><span>Rotate</span></div></div><iframe id="zm-display-iframe" src="page=mvc_landingpages&act=templatepage&preview=1&templateId=landingpage3" style="padding-top: 63px; margin: 0px auto;" scrolling="yes" width="100%" height="609"></iframe></div>
then, in java code i wrote :
this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("zm-view-frame")));
Then :
Tester.Browser.findElement(By.id("zm-view-frame")).click();
of course, i have defined this.wait :
public WebDriverWait wait = new WebDriverWait(Tester.Browser, 100);
and i get a wait timeout exception
I even use many type of By such as Xpath, css selector, class ... but still no luck.
I also use Selenium IDE for firefox to get element, export it to java code and view in my editor then do the same but it's not work for me again.
i'm really stuck on this, can you give me some help ?
and sorry for my bad english, many thanks !
UPDATE - this is my html structure :
html structure
UPDATE - I found the bug and have a solution
Reason : Before above code i have few line of codes to check if a iframe available, the code like :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).
I found the bug and have a solution
Reason : Before above code i have few lines of code to check if a iframe available, the code like below :
this.wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator));
and i didn't know that code make driver switched to that iframe, so my code above to find element is on wrong place (context).
Solution : so i need to back to defaut context (back to parent), i code like that :
Tester.Browser.switchTo().defaultContent();
Many thank to #Naveen to remind me to check that case :).
first time here and I tried to search for this info, so please forgive if this has been asked before. I am fairly knew to coding/selenium. I don't seem to have an issue with anything other than our SmartGWT screens and wondering if anybody could help with suggestions...I can't seem to get Selenium to work at all withing anything SmartGWT related actually
For example I have a date field and I want to type in a new date
This is the code according to firepath
<input id="isc_B" class="selectItemText" type="TEXT" tabindex="1131"
style="width:66px;height:12px;-moz-user-focus:normal;"
autocomplete="OFF" onselect="if (window.isc_ComboBoxItem_0 == null)
return;isc_ComboBoxItem_0.$54h()"
oninput="isc_ComboBoxItem_0._handleInput()" spellcheck="true"
$377="$378" $376="isc_ComboBoxItem_0" handlenativeevents="false"
name="valueField"/>
According to firepath these are the xpaths for it
[.//*[#id='isc_B']
html/body/div[4]/div[1]/div[2]/div/form/table/tbody[2]/tr[2]/td[1]/input
and I've tried these:
driver.findElement(By.xpath(".//*[#id='isc_B']")).sendKeys...;
driver.findElement(By.xpath("//*[#id='isc_B']")).sendKeys...;
driver.findElement(By.id("isc_B")).sendKeys...;
and can't get anything to recognize. any pointers would be awesome and thank you in advance. Do I need to import specific libraries for smartGWT or something?
Don't go by the XPaths suggested by FirePath..Try to create relative XPaths..Because absolute XPATHs can change..
For the above input tag I would recommend you use something like below
driver.findElement(By.className("selectItemText")).sendKeys("yourDate");
EDIT: Based on your website below is the code which works absolutely fine for me:
WebDriver driver=WebDriverFactory.getBrowser("http://www.tobymob.com/test/test.html");
driver.findElement(By.className("selectItemText")).sendKeys("someDate");
I am facing an issue with the file upload with WebDriver, using Java, on Firefox 24.
And I can NOT use some external program like AutoIT or similar.
I have to upload a file to a section which's HTML code is:
<td>
<input type="file" name="file">
</td>
And what I was using in Eclipse is
pageObject.getTypeFileLocation().sendKeys(textFile);
pageObject.getUploadButton.click();
which does not work; also tried the first answer of this question but neither did the trick.
The test returns 'OK', but I suspected that nothing was being done so added a check to wait for 'Upload complete' text present; But the file is not uploaded, and timeouts after 20 seconds, even when the file to upload is 5KB and takes less than a second when manually uploaded.
The input=file section contains a button and a 'No File Selected' text that changes to the filepath when a file is selected manually; and this is in what I am basing my idea that the file is not being upload; because the 'No File Selected' remains until the test fails.
I tried this on Chrome and seems to be working fine, and I know that there have been some reworks about input=file in FF since release 23, but mostly pointed to CSS styling, so I don't think it's related.
Also, a question that might sound kind of stupid, but questions are questions: Doesn't the sendKeys() action need a field to input those keys? I feel that the WebDriver is trying to write the path over a button, which can't perform the action as it's only a button.
Any help will be appreciated, and thanks in advance!
Solved it!
Don't know if it works for all browsers, but at least it does on FF and Chrome:
Found out that somebody used the FILE type, so reused it and got its absolute path:
protected File *fileName* = new File("*path to file*");
private String textFile = *fileName*.getAbsolutePath();
pageObject.getInput().sendKeys(textFile);
Hope somebody else finds it useful; as it's weird to be answering my own question.
I'm using Chromedriver to try and find the following element:
<td class="section-menuoption"
onmouseover="this.className='section-menuoption-hilite';"
onmouseout="this.className='section-menuoption';" align="left"
onclick="self.jssDetails.location='products.php?xUser=kery&xRand=123';">
Scheduled Changes
</td>
When using the SeleniumIDE in Firefox I can use this selector without issue:
//*[contains(text(), 'Scheduled Changes')]
However, when I attempt to use this selector with the Chromedriver I get a "no such element" error (running via maven). My Chromedriver code looks likes this:
WebElement box = driver.findElement(By.xpath("//*[contains(text(), 'Scheduled Changes')]"));
System.out.print(box.getText());
box.click();
I've tried different quoting strategies, different xpaths (that also resolve correctly in the SeleniumIDE), but with no success. The only thing I can think of now is changing my XPath implementation, but I'm not even sure that's possible with Chromedriver.
Any help would be very appreciated.
First, make sure it it not in any kind of frames. Otherwise you need switch into the frame first, like this.
Then try use css selector instead of XPath. For example (you may need try different variations):
WebElement box = driver.findElement(By.cssSelector("td[onclick*=\"self.jssDetails.location='products.php?xUser=kery&xRand=123';\"]"));
Also, you might want to post every XPaths you have tried, to help us analyse.
I have been working on java project which needs the selenium testing tool for running the test class. As per the requirement, I need to know how to write the xpath, and how to get the value (goodluck) using xpath for given tag.
<span class="clienthome" id="personTableForm:foundClientsTable:0:j_id118">
goodluck</span>
Please anyone kindly help me.
try - //span[#class='clienthome']/.[normalize-space(text())='goodluck']
or
//span[normalize-space(text())='goodluck']