Sendkeys not working on search inputfield - java

I am trying to access an input field with a dynamically changing ID, and using other ways to find the element doesn't seem to work either.
This is the input field:
<input data-v-44dd203d="" type="search" placeholder="Search users" class="form-control form-control-md" inputmode="search" id="__BVID__27">
This is my current code in Java:
driver.get("website");
driver.findElement(By.xpath("//input[#type='search']")).sendKeys("name");
I am not getting any error, but when i am running the code, the website opens but nothing happens to the field. Seems i cannot access it, if it makes sense.

I had similar issue once and adding a click to the element first and then send keys worked for me.

Related

Finding a specific attribute, Selenium Xpath Java

I have the below code that I am attempting to extract a specific attribute from(data-id).
I am new to using selenium and have been pained by this for over a day now.
To add context to this I will give you a little background into what I am trying to achieve.
I have a webpage that has a auction, the auction has an ID, all items in the auction have unique ID's but all link to the original Auction ID.
I am attempting to extract the "data-id" attribute of an element however I have yet to find out how.
Here is a snippet of the code I am attempting to get the id from.
<div class="dropdown open">
<a class="dropdown-toggle form-control" href="#" data-toggle="dropdown">
<ul class="dropdown-menu dropdown-menu-form" role="menu">
<li id="liAuction4c42556772376a443736343d">
<label class="checkbox">
<input id="chkAuction4c42556772376a443736343d" class="auction" type="checkbox" data-caption="09-10-2015 10:30:00" data-id="4c42556772376a443736343d" checked="checked"/>
09-10-2015 10:30:00
</label>
</li>
</ul>
</div>
I have been on many forums and searched the whole of google and not found a solution that has worked for me yet otherwise I would not be posting the question and look like a complete Rookie.
I have attempted to use .getAttribute however I have had some issues with that and the code has never compiled, I guess I have not done something correctly.
String dataID = selenium.findElement(By.xpath("//*[starts-with(#id, 'liAuction')]")).getAttribute("data-id");
When I attempted the above the "findElement" part is underlined red and I have the following message,
"The method findElement(By) is undefined for the type Selenium".
If I change the parentisis around to look like this;
String dataID = selenium.findElement(By.xpath("//*[starts-with(#id, 'liAuction')]").getAttribute("data-id"));
"findElement" is no longer underlined, however now the ".getAttribute" part is underlined red and I have the following message, "The method getAttribute(String) is undefined for the type By"
I would really appreciate some assistance with this as I am about to throw my laptop out of the window, and I don't really want to lose all of my files.
Thanks in advance
Tony
You can use the getAttribute method.
First find the input element, then extract the data-id:
WebElement inputElement = selenium.findElement(By.id("chkAuction4c42556772376a443736343d"));
String data-id = inputElement.getAttribute("data-id");
Use below xpath:-
//input[#id='chkAuction4c42556772376a443736343d']/#data-id
Then use:-
String dataID = selenium.findElement(By.xpath("//input[#id='chkAuction4c42556772376a443736343d']/#data-id")).gettext();
Full Code:-
static WebDriver driver=null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.get("URL");
String dataID = driver.findElement(By.xpath("//input[#id='chkAuction4c42556772376a443736343d']/#data-id")).gettext();
System.out.println(dataID);
}
Hope it will help you :)
Thank you for your hekp with this guys, was not expecting to get the answer quite so quickly.
I needed to use the following line to retrieve the "data-id" attribute of an element.
String dataID = selenium.getValue("//input[#id='chkAuction4c42556772376a443736343d']/#data-id");
This ended up being alot easier that initially thought, I would like to thank #Shubham Jain for help with this his suggestion pointed me to where I needed to be.
I hope this helps others in the future
Hi I suggest you to use the following code. It will work absolutely fine.
WebElement element = selenium.findElement(By.xpath("//input[#class='auction']"));
String dataId= element..getAttribute("data-id");
Detail
I'm identifing the object using following xpath //input[#class='auction'] and save it to an WebElement variable.
then By using getAttribute() im getting the string from data-id

selenium web driver : Unable to locate element

Well not to mention that I'm new on using the selenium web driver, I'm trying to automatize a mailbox so I can log myself in and to send a message, the thing is that I download the firebug plugin for Mozilla, that is the browser I'm currently working with, when I'm trying to find the element for the log in and the password everything goes well, because I do it by their name.
input class="_nb-input-controller" type="text" **name="login"** autocorrect="off" autocapitalize="off" value=""
input class="_nb-input-controller" type="password" **name="passwd"** value=""
but as soon as im at the mailbox entry the identificator changes it, i go to compose link and this is the code that shows up:
a class="b-toolbar__item b-toolbar__item_compose js-toolbar-item-compose daria-action" title="Compose (w, c)" href="#compose" data-action="compose.go" data-params="toolbar=1&toolbar.button=compose"
Since I don't find any method that could help on this, a friend came and told me about the xpath which was:
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
by doing this selenium did find the element and I could click on the icon,
but again.. when I'm trying to get into the "TO" prompt so I can write inside
it is not finding the element, not by xpath, or anything is there a way or formula to get elements or translate them into a way that selenium can find them???
oh i see, i re try with the element and i noticed that the one i wanted was showing as hidden using firebug so i just try the next one up and the web driver could locate the item, it was a simple app to send an email the prompt that i wanted to locate was the 'TO' field. and thanks guys for giving me the correct way of using the Xpath property
Having locators such as :
/html/body/div[2]/div/div[5]/div/div[3]/div/div[2]/div/div/div/div[2]/a[2]
is generally a bad idea. If you want to build a stable test I would suggest you stay away from such.
Now I see you have attributes like #name and #class. XPath is provides you with a way to reach these elements directly by pointing the identifiers instead of the whole path to the element.
You can try something like:
//input[#name='login']
//input[#name='password']
More info at: http://www.w3schools.com/xsl/xpath_intro.asp

How to click on an element using its Id and Value attributes together?

I am trying to click on some button (which becomes enabled after all of the fields are fill in):
<div class="savCancelContainer">
<input type="button"
value="Save"
translatekey="ACTVITY_DETAILS_SAVE_BUTTON"
class="translate" id="submitActivityDetails"
style="background-color: rgb(0, 125, 195);">
The programmers of the web-page have changed it for some reason, and now my code is no longer working correctly (the button doesn't get clicked on):
driver.findElement(By.id("submitActivityDetails")).click();
I also tried finding it by xpath, with no success.
Is there any way to click the button using the Id and Value attributes together?
Any other ideas?
Similar pages and dialogs are still working fine...
You need to create a xpath which will contain both the attribute:
//input[#id='submitActivityDetails'][#value='Save']
And Click event can be triggered in the following way:
driver.findElement(By.xpath("//input[#id='submitActivityDetails'][#value='Save']")).click();
Lemme know if it helps!
Additionally you can use css seelctor to perform that action too.
[id='submitActivityDetails'][value='Save']

Select invalid html-tag with Selenium

I am trying to get a WebElement with Selenium:
driver.findElement(By.xpath("//input[#name='j_username']"))
But Selenium says: "Unable to find element with XPath ...".
The XPath is valid, I proofed it with FirePath.
But the input element has the following invalid code:
<input size="10" type="text" name="j_username" maxlength="8">
I can't change the html-file, despite the fact is there any solution to get the webElement?
Thanks in advance!
try select element with css selector. and also verify in firepath(firebug addon that element is located properly).
so your css selector be something like
input[name='j_username']
2nd approach is to use internal firebug mechanism for finding xPaths of elements.
See screen attached below
After these manipulations driver shoulda handle element properly.
Well I will suggest adding an id to your html code -
<input id="j_username"size="10" type="text" name="j_username" maxlength="8">
and findElement by id -
driver.findElement(By.id("j_username"));
I have faced similar issues with xpath(borwser issues??) but id never fails for me. ;)
By the way I feel your code should be -
driver.findElement(By.xpath(".//*[#name='j_username']"));
The best solution is to find out what selenium is doing wrong, but without a URL or sample page to test on it's a little hard. Is there anyway you could dump the HTML into a jsfiddle? If there is do that and paste the url into the question and I'm sure someone can find a solution.
If not however, another way to get the results is to do it with jQuery. If firebug is picking it up but not selenium, then there's no reason why jQuery wouldn't get it. Here's how to go about doing that if needed:
Step 1: Is jQuery already present on the page? If so then you don't need to do this bit, otherwise you will need to add it yourself by using driver.executeScript(addjQueryScript) where the script does something like this.
Step 2: call WebElement input = driver.executeScript(elementSelector); where the elementSelector script would be something like \"return $('input[name=\"j_username\"]')\");
My jQuery's not so good, but I believe that should work...
Best of luck!

Java Selenium Firefox Driver - Textbox Onchange Issue

Running Java/Selenium 2.3 (and 4) using Firefox Driver on centos
Trying to test against a site that has form with a text type, with an onchange. Tried to insert the text using the sendkeys, and then changing focus by doing a select/click on another term in the form. Tried to do a fireevent as well. (Doesn't appear to be supported in the 2.3 sel)
Searched the net as well with no luck.
Basically, trying to get a solution to how to do an insert into a textbox for selenium/firefox driver, so the inserted text actually appears in the textarea, which means the onchange event gets fired.
The test html is::
<td rowspan='4' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divCLASS_SRCH_WRK2_SUBJECT$69$'><input type='text' name='CLASS_SRCH_WRK2_SUBJECT$69$' id='CLASS_SRCH_WRK2_SUBJECT$69$' tabindex='31' value="" class='PSEDITBOX' style='width:60px; ' maxlength='8' onchange="addchg_win0(this);oChange_win0=this;" />
</DIV></td>
The test code is::
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("ACG");
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("");
Select sCourse= new Select(driver.findElement(By.id("CLASS_SRCH_WRK2_ACAD_CAREER")));
sCourse.selectByValue("");
The test sets the textelement, and then sets the select/option of a select item, which should trigger the change in focus. I also tried to clear, and reset the text, thinking that might trigger the onchange..
A solution to this would help a lot of people who've been looking for the same thing!!
Thanks
Have you tried doing a tab after sendKeys?
You can do a
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("\t");
I had a similar problem (though my trigger was onBlur). I called the blur() method directly. In your case it would be
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$")).sendKeys("ACG");
((JavascriptExecutor)driver).executeScript( "$('[name=\"CLASS_SRCH_WRK2_SUBJECT$69$\"]' ).blur() );
You might need a different function other than blur().

Categories

Resources