How to find placeholder value in Selenium - java

I need to verify whether placeholder/helper text is displayed for a field.
In this example it is Min. $50.00
I don't see any placeholder/helper text attribute defined inside the div tags.
My field looks like . Displayed amount (in this example $50.00) is dynamic.
Code for the above field looks like
<div class="unit" style="padding-left:32px;">
<input id="Amount" type="text" class="inputAlign optionalHintText" size="18" maxlength="30"/>
<div id="sharesValueForPercent" class="TextMd"></div>
</div>
I tried different ways to get the text (Min$50.00). Can someone help me on this. Appreciate your response.

To extract the placeholder text i.e. $50.00 you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(driver.findElement(By.cssSelector("input.inputAlign.optionalHintText#Amount")).getAttribute("value"));
Using xpath:
System.out.println(driver.findElement(By.xpath("//input[#class='inputAlign optionalHintText' and #id='Amount']")).getAttribute("value"));

Related

I was able to run my program till morning. But suddenly getting this error " Driver info: driver.version: unknown [duplicate]

I am getting the following error:
"Compound class names not permitted"
While trying to access web element where-in the element's class name have spaces in between. The page source for the web element is as below.
driver.findElement(By.className("alert alert-success"));
<div class="alert alert-success" alert-dismissable"="" id="58417" style="display: none;">
<button type="button" class="close hide-panel close-icon-58417" data-dismiss="alert" aria-hidden="true" style="display: inline-block;">×</button><span id="caret-58417" class="notification-caret caret-58417"></span>
<div class="hide-panel close-icon-58417" id="58417" style="display: block;">
<span class="glyphicon glyphicon-ok-sign"></span><strong>Success</strong> KeyLinks Updated Successfully
<div class="notification-panel-body panel-body-58417">REST Invocation Success</div>
</div>
</div>
I tried to find the element by CSS path as below. But the element is not searchable with this.
driver.findElement(By.cssSelector(".alert alert-success"));
This was the workaround given in the Link but still no success. your help will be much appreciated.
You can access the element if it has multiple classes using "By":
from selenium.webdriver.common.by import By
driver.findElement(By.cssSelector(".alert.alert-success"));
You can use
driver.findElement(By.className("alert-success"));
or
driver.findElement(By.className("alert"));
As right now selenium doesn't support multiple class name.If your class name includes a space, WebDriver will see it as a "compound selector".
You can use cssSelector or id for selecting the webelement.
If usage of class name is must you can use the following ways:
1) css selectors:
driver.findElement(By.cssSelector(".alert.alert-success");
2) using xpath
driver.findElement(By.xpath("//div[#class='alert alert-success']"))
Try avoiding xpath and use css selectors instead.
The issue is because of the way find by Class name works.
in your code class name is class="alert alert-success"
If the class name has space you'll get the above error. You can simply get rid of the issue by using Id, CSS, Xpath, regular expression or any other element finder method.
Do you need to use Class Name or can you use another method? Let me know if you need to use class name.
It can also be done using class name like:
driver.find_element_by_class_name("alert")
or
driver.find_element_by_class_name("alert-success")
you can select anyone class name from two or more class names separated by spaces it'll work just fine.
Most of the time class name attribute of any element has groups of classes names.
like class = 'alert alert-success another class name'
if you are using css selector ,then just remove spaces between the class names and create you clas name like below :
driver.findElement(By.cssSelector(".alert.alert-success.another.class.name")
. => represent class
=> represent ID
for CSS selector.

Cant find dynamic xpath using chropath : It might be child of svg/pseudo element/comment/iframe from different src.Currently ChroPath doesn't support

I am trying to use xpath to find input in a textbox a card number but the id is dynamic and class contains multiple hyphens , its not able to find the xpath i have tried for the given html
<div class="a-section a-spacing-none add-cc-number
<input type="tel" id="pp-XQAs3V-15" autocomplete="off" name="addCreditCardNumber" class="a-input-text a-form-normal a-width-medium" data-testid="pmts-account-Number">
</div>
</div>
When i use chropath it says It might be child of svg/pseudo element/comment/iframe from different src. Currently ChroPath doesn't support for them.
Xpath tried:
//input[contains(name(),'ppw-accountHolderName')]
tried with ancestor sibling too.
name() is an XPath function to get te name of een element.
If the input has this name addCreditCardNumber use this:
//input[#name='addCreditCardNumber']

Select/click on checkbox by labelin Selenium

I have the following html code and would like to select checkbox by label:
<input type="checkbox" onclick="searchResult(this,'8')" id="catalog-8"/>
<label for="catalog-8">
my-assortment </label>
in the above example, by the value "my-assortment"
I tried this: //label[containts('my-assortment')] but it does not work.
More exactly, I want to write something like this:
//input[#type='checkbox'] which has "id" of the value of "for"
in label[contains(., 'my-assortment')]
Does anybody have any idea?
//label[containts('my-assortment')]
First of all, this is not containts - it should be contains (watch the extra t). And, you are not using contains() correctly. It should be:
//label[contains(., 'my-assortment')]
Also, if you want to click the input element by label, preceding-sibling would help here:
//label[contains(., 'my-assortment')]/preceding-sibling::input[#type='checkbox']

Sendkeys to a GXT integerSpinnerField in Selenium

I have been trying to "sendkeys" to a IntegerSpinnerField but it is not working. The IntegerSpinnerField has a id but I can't use the "sendkeys" on it. I opened up the html the ID is there but the IntegerSpinnerField is 3 widgets in 1. So is there anyway i can give an id to the 3 widgets that make up the IntegerSpinnerField.
html-code
<div __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true" id="maximumfrequencySpinnerField" class="GJCAMDBBOPB" title="Enter an maximum frequency value within the allowable range. The allowed range is 776 to 787 MHz" style="width: 100px;"><div style="width: 100px;" class="GJCAMDBBAW"><table width="100%" cellpadding="0" cellspacing="0"><tbody><tr><td><input style="width: 75px;" type="text" value="" class="GJCAMDBBCV GJCAMDBBJV" id="x-auto-229-input" tabindex="0" disabled=""></td><td><div class="GJCAMDBBKV"></div><div class="GJCAMDBBNV"></div></td></tr></tbody></table></div></div>
Id of the textfield which is autogenerated
Don't go with id's for ExtJs Applications those were dynamically generated at runtime and prone to change everytime.So you have to write a relative xpath for every element.And ExtJS elements follow some pattern So you have to write xpaths for that and you can reuse them.
Refer any-suggestions-for-testing-extjs-code-in-a-browser-preferably-with-selenium
For your case and for most input element you can find it using label.The immediate input of the label might be the element you are looking for
I have tested the spinner field named Revenue %: in this demo site using the following xpath
//label[text()='Revenue %:']//following::input
The script for the above site.Hope it helps
WebDriver driver = new FirefoxDriver();
driver.get("https://www.sencha.com/examples/#ExamplePlace:dashboard");
WebElement element=driver.findElement(By.xpath("//label[text()='Revenue %:']//following::input"));
element.clear();
element.sendKeys("67.67");

How to get the text within a <span> tag knowing its ID with java and jsoup?

im tring to get the text within a tag. I know its id, which is "lblCompra" and "lblVenta".
Document doc = Jsoup.connect("http://www.bccr.fi.cr//").get();
Element compra = doc.getElementById("lblCompra");
Element venta = doc.getElementById("lblVenta");
System.out.println(compra);
System.out.println(venta);
But what i get is bunch of text I dont need. Here's the output:
<span id="lblCompra"> <img src="images/waiting.gif" alt="cargando" /> </span>
<span id="lblVenta"> <img src="images/waiting.gif" alt="cargando" /> </span>
what i should be getting is like "400.0" and "450.0" or something like that.
In order to get text value of an element try using text() method:
System.out.println(compra.text());
UPDATE: the value you are looking for is loaded by JavaScript.
Consult this topic
if you use getElementById() ...you will get the element. Now that you have the element get the value from the element. Use the getValue() method to get the value
Maybe doc.getElementById("#lblCompra").innerHTML will work?
Or if you want to get width and height of img, you can do this:
doc.getElementById('#lblCompra').getElementsByTagName('img')[0].width;

Categories