xpath grab value of listbox options in google sheets - xpath

Can you output a list of option values in a select tag with XPath? I am only able to grab the labels. Can you actually output an attribute of a tag with XPath? I am using importxml in Google Sheets but if there is another way to accomplish this that is my problem.
example
<select name="store".... <option value = 101>store #1</option> .... etc etc
I want to return a list of all the values not the actual label between the tags... can this be done with XPath? If not what is another way to do it with the import functions in google sheets?

to receive the value of an attribute do #nameofattribute like so...
=importxml("yourwebsite.com","//select//#value")

Related

How to get only div text not span text using xpath

I am trying to get only order numbers like "190795". check the image
What i have tried
self.driver.find_element_by_xpath('//div[#class="sc-kafWEX ihwrOP"]//div/div/div[3]').text
But it will return span text such as "Order number:190795" like this.
I want only "190795"
This is my HTMl code
That is a text node, you can not write an XPath (v1.0) for that, and Selenium make use of XPath v1.0 , so you will have to be dependent on binding language.
Try this:
org_text = self.driver.find_element_by_xpath('//div[#class="sc-kafWEX ihwrOP"]//div/div/div[3]').text
desired_text = org_text.split(':')[1]
print(desired_text)

Why does IMPORTXML with XPATH return unexpected blank row in addition to expected result?

I'm importing into Google Sheets with IMPORTXML with the following XPATH:
=IMPORTXML(A2;"//*[#id='mw-content-text']/div/table[1]/tbody/tr[4]/td[1]/ul/li")
A2 containing the URL (https://stt.wiki/wiki/20th_Century_Pistol).
From the website I want to import the list entries in the "Basic" column and "Crafted From" row of the table.
There are only two list entries in this section of the table:
"x1 Basic Security Codes" and
"x4 Basic Casing"
Therefore, I expected to get only those two list entries as rows in my sheet.
Instead, I got an additional blank row above those two entries. When I change "td[1]" to "td[3]" in the XPATH query however, there are no extra blanks.
I don't understand where the additional blank row is coming from and how I can avoid it.
Google Sheet with desired and actual result
When I saw the HTML of the URL, there are 2 li tags in the ul tag. So I think that your xpath is correct. But from your issue, I was worry that the sup tag might affect to this situation. But I'm not sure whether this is the direct reason. So I would like to propose to add the attribute of li for your xpath as follows.
Modified xpath:
When your xpath is modified, please modify as follows.
From:
//*[#id='mw-content-text']/div/table[1]/tbody/tr[4]/td[1]/ul/li
To:
//*[#id='mw-content-text']/div/table[1]/tbody/tr[4]/td[1]/ul/li[#style='white-space:nowrap']
By adding [#style='white-space:nowrap'], the value of li with style='white-space:nowrap' is retrieved.
Result:
The formula is =IMPORTXML(A1;"//*[#id='mw-content-text']/div/table[1]/tbody/tr[4]/td[1]/ul/li[#style='white-space:nowrap']"). Please put the URL to the cell "A1".
Note:
Also, you can use the xpath of //*[#id='mw-content-text']/div/table[1]/tbody/tr[4]/td[1]/ul/li[position()>1].
To complete the very neat #Tanaike's answer, another expression :
=IMPORTXML(A2;"//th[contains(.,'Crafted')]/following::td[1]//li[contains(#style,'white')]")
If a blank line is added it's because GoogleSheets parses an additional blank li element containing a #style attribute.

Retrieve value from within a div tag in xpath

I am trying to retrieve the value in the data-appid field, I have tried using following-sibling but its not really a sibling per se. Not sure how to go about retrieving this. Any pointers will really be great.
<div class="section app" data-appid="532054761" data-updateid="10184169">
The sibling axis applies to elements, not attributes.
You can reference data-appid simply as an attribute of the div element. For example,
//div/#data-appid
will select 532054761
If you need to be more specific about the particular div element for which you want its data-appid, you can use a predicate to select a particular div element. For example:
//div[#data-updateid='10184169']/#data-appid
You need to use getAttribute() method
Try the following code
String dataAppId = driver.findElement(By.xpath("//div[#class='section app']")).getAttribute("data-appid");
System.out.println(dataAppId);

How get AJAX elements which XPATH is varying dynamically

In my application one AJAX filed which is displaying some values
When I tried to find out their XPATH it is varying dynamically
for example :
First time when I tried to find out the path it is giving it as .//*[#id='ix-rt-13']. When I refresh the page it is giving it as .//*[#id='ix-rt-6'].
Actually it is displaying 2 values one with id .//*[#id='ix-rt-13'] and second one with .//*[#id='ix-rt-14']. And when I refresh the page it is giving XPath values as .//*[#id='ix-rt-6'] and .//*[#id='ix-rt-7'].
I want to retrieve the second element text. How to do that ?
<li class="ui-menu-item" role="presentation">
<a id="'ix-rt-15" class="ui-corner-all ui-state-focus" tabindex="-1">Being Powerful</a>
</li>
If I understand the question correctly, you can try this XPath :
(//*[starts-with(#id,'ix-rt-')])[2]
Above XPath will search for all elements with id attribute value starts with 'ix-rt-', then return the 2nd result.
If link text does not change, you shouldn't use xpath rather use link text:
driver.findElement(By.linkText("Being Powerful")).click();
However if you bend upon using xpath you may try following:
driver.findElement(By.xpath("//a[contains(#id,'ix-rt-')]")).click();
But this would select the 1st element containing 'ix-rt-' in its id. So it may not work as desired if there are more than 1 such elements. In that case, if you know the index of element on page, you may use following:
driver.findElement(By.xpath("(//a[contains(#id,'ix-rt-')])[2]")).click();

xpath search for divs where the id contains specific text

On my HTML page I have forty divs but I only want one div.
Using agility pack to search and get all the divs with Ids I use this XPath:
"//div[#id]"
But how do I search for divs with Ids where the id contains the text "test" like so:
<div id="outerdivtest1></div>"
Use the contains function:
//div[contains(#id,'test')]
I've used this with for the CSS class:
//div[#class = 'atom']
I assume it's similar with id's.
You can use the xpath
//div[#contains(#id,'test')]
If you want to use the first occurrence, it works fine but if it's not the first occurrence you have to go with different xpath specific to the particular element.
For example if you want first selection "id" is like that
(//[#id="numberIdentify"])//following:://a[contains(text(),'V ')]

Resources