Can't locate some text using xpath from certain elements - xpath

Is it possible to locate the name within "value" attribute from the below elements using xpath:
<div class="div_input_place">
<input name="txt_name" type="text" value="ANWESH SHETTY" maxlength="20" id="txt_name" disabled="disabled" tabindex="2" class="aspNetDisabled textboxDefault_de_active_student">
</div>
I've tried with the below expression but found no result:
//input[#value]/text()

//input[#value]/text() would locate an input element which has value attribute, then it would get you the "text" of the element - which is empty.
What you need instead is to locate the input element, for instance, by id and then get the value of the value attribute:
//input[#id = "txt_name"]/#value

Related

How to set tabindex attribute

When using Binding.scala, I can not write html that uses the tabindex attribute.
Is this a bug in Binding.scala / scala.js?
<div>
<input tabindex="1"></input>
<input tabindex="3"></input>
<br></br>
<input tabindex="2"></input>
<input tabindex="4"></input>
</div>
Results in compile error:
ScalaFiddle.scala:12: error: value tabindex is not a member of scalajs.this.dom.html.Input
ScalaFiddle.scala:13: error: value tabindex is not a member of scalajs.this.dom.html.Input
ScalaFiddle.scala:15: error: value tabindex is not a member of scalajs.this.dom.html.Input
ScalaFiddle.scala:16: error: value tabindex is not a member of scalajs.this.dom.html.Input
I tried to use the attribute (or property?) tabIndex, but it is not a string and the attribute argument needs to be a string.
For example see this: https://scalafiddle.io/sf/kDg2uAA/0
I am quite new to scala, sbt and scala.js, so I am not sure where/how to fix this and how to test a fix locally before creating a pullrequest.
You can use the tabIndex attribute with a value enclosed in {} (tip: you can use any scala code inside!).
<div>
<input tabIndex={1}></input>
<input tabIndex={3}></input>
<br></br>
<input tabIndex={2}></input>
<input tabIndex={4}></input>
</div>
Please, see a full code here: https://scalafiddle.io/sf/hGkAVib/1
You need to use the data:tabindex property. Please see https://scalafiddle.io/sf/TlcSdfF/1

Find two text field that seem to have the same class using selenium ruby

I am writing a simple script to find and enter text into First name and Last name fields for a web page profile in firefox. I have navigated to the page and inspected the element, however I have tried everything i can think of the find the element but nothing seems to work. How can I find these fields using ruby, is there another way to inspect the field?
Both of these fields have field labels
First name inspect result
<div class="input__label">First Name</div>
<div class="input_row">
<div class="split_control">
<input class="input__input_wide" step="any" value="Bob">
Last name inspect result
<div class="input__label">Last Name</div>
<div class="input_row">
<div class="split_control">
<input class="input__input_wide" step="any" value="Smith">
You can try with following x-paths:
First Name element: //div[text()='First Name']/following-sibling::div/div/input
Last Name element: //div[text()='Last Name']/following-sibling::div/div/input
In code,
first_name = driver.find_element(:xpath, "//div[text()='First Name']/following-sibling::div/div/input")
first_name.send_keys "FName"
last_name = driver.find_element(:xpath, "//div[text()='Last Name']/following-sibling::div/div/input")
last_name.send_keys "LName"
Write the xpath like given below
driver.find_element(xpath: "//div[normalize-space()='First Name']/following::input").send_keys "FirstName"
For LastName
driver.find_element(xpath: "//div[normalize-space()='Last Name']/following::input").send_keys "LastName"

How do i match the value which has a radio button checked

I have a list of similar looking DIVs without any Div ID, except one has a check box checked and others doesn't. What i need is to find the value from a child tag only if a radio button is selected.
Below is a simpler version of my code.
<div class = "XYZ">
<input type="radio" checked>
<input type="hidden" value="This is a great thing 1">
</div>
<div class = "XYZ">
<input type="radio">
<input type="hidden" value="This is a great thing 2">
</div>
Result needed is
This is a great thing 1
Unfortunately the source code cannot be changed.
Your xpath should look for a div that contains the checked input and to get the value for the one that has value attribute.
First selector returns the input, the second returns the value.
//div[.//input[#checked]]/input[#value]
//div[.//input[#checked]]/input/#value
As an alternative you can use the following sibling:
//input[#checked]/following-sibling::input
If you want to also use the class of the parent div:
//div[#class='XYZ']/input[#checked]/following-sibling::input

CSS selector path for text box that comes immediately after h3 with text "Company Name"

Below is my html
<div class="right" data-bindattr-13="13">
<h3>Company Name</h3>
<div class="input-row">
<input id="ember4258" class="ember-view ember-text-field" type="text"/>
</div>
<h3>Full Company Legal Name</h3>
<div class="input-row">
<input id="ember4259" class="ember-view ember-text-field" type="text"/>
</div>
<h3>Company Phone</h3>
<div class="input-row">
<input id="ember4260" class="ember-view ember-text-field" type="text"/>
</div>
<h3>Federal Tax / Employer ID (EIN)</h3>
<div class="input-row">
<input id="ember4261" class="ember-view ember-text-field" type="text"/>
</div>
</div>
Since class value off all the text fields and respective parent div class attributes are same I need to fill these text fields without using nth-of-type.
I have done work around to create a CSS selector that should point the text box that is immediately after <h3>Full Company Legal Name</h3> .
h3:contains(^Company Name$)+div>input
But my Capybara script is not recognizing the above way and throwing the below error.
annotateInvalidSelectorError_': The given selector h3:contains(^Company Name$)+div>input is either invalid or does not result in a WebElement. The following error occurred: (Selenium::WebDriver::Error::InvalidSelectorError)
InvalidSelectorError: An invalid or illegal selector was specified
Can any one provide a CSS that matches my requirement?
Regards,
Avinash Duggirala
The contains pseudo class was deprecated, which is why the InvalidSelectorError occurs. There is currently no way to check text nodes using CSS-selectors.
Instead, you can use XPath to traverse the DOM to sibling elements.
text_field = page.find(:xpath, '//h3[text() = "Company Name"]/following-sibling::div[1]/input')
text_field.set('some value')

Xpath: select an element of specific type which is sibling to the selected element

Here is an excerpt of my xml:
<div class="0001">
<label title="Label1">Label1:</label>
</div>
<div class="0002">
<input type="text">
</div>
I know how to select the "Label1" label. I need to select an input of text type. Class names are generated in a random way and changes after each session. So it's only possible to refer to label title and an input type
Try:
'//div[label[#title="Label1"]]/following-sibling::div/input[#type="text"]'
select a <div> which contains a <label> which itself has a title attribute with value Label1, then look for an <input> with type "text" in the <div> siblings of this former <div>

Resources