$x("//div[#class='card-info__container']/div[3]/a")
using this I've found the container, but now I can't get the specific element I need.
when I enter this the response is:
⯆(3) [a.cta.btn, a.cta.btn, a.cta.btn]
⯈0: a.cta.btn
⯈1: a.cta.btn
⯈2: a.cta.btn
how can I take only the first one?
You can use the at() method to select the first item from the array of a that is returned from executing the XPath:
$x("//div[#class='card-info__container']/div[3]/a").at(0)
For an XPath that returns a list of elements,
xpath
you can select only the first element via indexing:
(xpath)[1]
Related
<tr><strong>dynamic title</strong></tr>
<tr>1</tr>
<tr>2</tr>
<tr>3</tr>
<tr><strong>static title</strong></tr>
<tr>4</tr>
<tr>5</tr>
<tr><strong>dynamic title</strong></tr>
<tr>6</tr>
<tr>7</tr>
<tr><strong>dynamic title</strong></tr>
<tr>8</tr>
<tr>9</tr>
Given the above scenario, I want to select the values 4 and 5 using the static title as a marker.
//tr[preceding-sibling::tr[strong][contains(.,"static title")]] or
//tr[strong[contains(.,"static title")]]/following-sibling::tr
This will select 6,7,8 and 9 too.
Is there a way to make it select the first preceding-sibling with strong and then check for the contains? Or maybe we can use count() somehow?
You can insert [1] after strong which means "the first strong preceding sibling must be static":
//tr[preceding-sibling::tr[strong][1][contains(.,"static title")]]
This looks little messy, but should work:
//tr[contains(strong,"static title")]/following-sibling::tr[strong][1]/preceding-sibling::tr[preceding-sibling::tr[contains(strong,"static title")]]
Trying to select the Export button below which has an ID and name. I know I can getelementbyID but not sure how to then specify to further drill into the getattribute("name") because the getelementbyID wil only return 1 value, not an array
Thanks!
#Mturks83
getElementsByName("export")(0) the first part will give you the array of elements called export the (0) gives you the first.
It is faster to use querySelector than getElementsBy.
For example, just got direct with an attribute = value selector
ie.document.querySelector("[name=export]")
If more than one then add the parent td element id
ie.document.querySelector("#bottomButtonRow [name=export]")
Reading:
css selectors
Using findElements() and size() methods we can get the count.
But I want to extract the count using count() function in the xpath.
Will the count function return an integer value?
Suppose,
My xpath is (//input[#id='stack'])[3] and there are 3 matching nodes with //input[#name='hai']
Can I modify my xpath like below?
(//input[#id='stack'])[count(//input[#name=''hai])]
Yes, if
count(//input[#name='hai'])
evaluates to
3
then
(//input[#id='stack'])[count(//input[#name='hai'])]
will select the same nodes as
(//input[#id='stack'])[3]
would select.
However, your intent is quite unclear, especially given that
//input[#id='stack'] will select all of the input elements with
an id attribute value of 'stack'. Usually id attribute values
are unique across the document, so usually this would select only a
single element.
(//input[#id='stack'])[count(//input[#name='hai'])] assumes that there are at least as many input elements id'ed as 'stack' as there are input elements named 'hai' -- an odd assumption.
driver.findElements() returns a List of WebElements, and .size() returns the integer size of a list, so I think youd be better off doing the following:
int myCount = driver.findElements(By.xpath("<Your Xpath Here>")).size();
This will get you the count of elements on your page that match your xpath input
I am trying to find a way to return the index of a HTML child tag based on its xpath.
For instance, on the right rail of a page, I have three elements:
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[1]/h4
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[2]/h4
//*[#id="ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880"]/div[3]/h4
Assume that I've found the first element, and I want to return the number inside the tag div, which is 1. How can I do it?
I referred to this previous post (How to count HTML child tag in Selenium WebDriver using Java) but still cannot figure it out.
You can get the number using regex:
var regExp = /div\[([^)]+)\]/;
var matches = regExp.exec("//[#id=\"ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880\"]/div[2]/h4");
console.log(matches[1]); \\ returns 2
You can select preceeding sibling in xpath to get all the reports before your current one like this:
//h4[contains(text(),'hello1')]/preceding-sibling::h4
Now you only have to count how many you found plus the current and you have your index.
Another option would be to select all the reports at once and loop over them checking for their content. They always come in the same order they are in the dom.
for java it could look like this:
List<WebElement> reports = driver.findElements(By.xpath("//*[#id='ctl00_ctl50_g_3B684B74_3A19_4750_AA2A_FB3D56462880']/div/h4")
for(WebElement element : reports){
if(element.getText().contains("report1"){
return reports.indexOf(element) + 1;
}
}
Otherwise you will have to parse the xpath by yourself to extract the value (see LG3527118's answer for this).
I've got a select element in my HTML page and I'd like to do a specific action using Mootools when a particular value of this select is chosen but I don't know how to access the element.
My element has cars_number as id and to access value 1 of this select element I've tried cars_number[1] but it doesn't work.
Try with:
$('cars_number').getSelected();
MooTools Documentation