Selenium and xpath: Clicking a checkbox in one table's column, based on text in another? - xpath

Basically there is a table with names, edit buttons, and a checkbox at the end column that I want to check on with selenium. But I want to make sure I click on the one I created with selenium and that's where my problems begin.
Using the selenium IDE, the names xpath is
//tr[5]/td[2]
The checkbox is
//tr[5]/td[4]/input
So the text is in column 2 and the box is column 4, and my record would be the 5th one. but I cannot for the life of me get ANY text search to work. Even something basic like
<tr>
<td>storeText</td>
<td>//tr[contains(text(), 'McGowan')]/td[2]</td>
<td>text</td>
</tr>
Even if the text matches identically, it gives me the locator not found error. No matter what combination i use to find xpath by text it has never worked, and ive spent quite a few hours reading every combination out there.
We are using the IDE and the RC in html, so no java or any other exporting.
Thank! (My first post!)

//td[text()='McGowan']/../td/input[#type='checkbox']
Let me know if this works for you!

This might be odd, but the coment below the answer, regarding a random click that let to the answer ---> //tr[contains(., 'text')]/td[3]/a <--- was just randomly verified as exactly what I needed.
Good job guys.

Related

Setting the correct xpath

I'm trying to set the right xpath for using RSelenium, but I'm not very experienced in this area, so any help would be much appreciated.
Since I'm not allowed to post pictures yet I have tried to add a link to a screenshot of the html:
The html
I need R to scrape the dates (28-10-2020 - 13-11-2020), but so far I have not been able to set the correct xpath when using html.nodes.
I'm trying to scrape from sites like this one: https://www.boligsiden.dk/adresse/topperne-9-3-33-2620-albertslund-01650532___9__3__33
I usually do this on python rather than R
As you can see in this image when you right-click on the element concerned. You get a drop-down menu with an x-path to the element.
Other than that, the site orientation and x-path might change and a full x-path might be a good option in the short-run, so I rather prefer driver.find_element_by_xpath('//button[contains(text(),"Login")]')\ .click()
In your case which would be find_element_by_xpath('//*[contains(#class, 'u-pb-4 u-block')]')
I hope this helps and it is mostly the same across different languages

Verifying search filtering in Ruby Selenium Webdriver

I'm very new to Ruby, Selenium, and UI automation, and have a quick question on how to get a count of visible items in a drop down menu.
Example: I have a drop down menu of 10 currency values (USD, EUR, JPN, etc.). They're coded as:
<div class="list_item">Currency Symbol</div>
The drop down menu is searchable, and if I type in "USD", then the only visible item would be that particular currency value. All other div of that class get a style="display: none;" attribute. How do I verify that that USD indeed is the only item in the menu? An example of such a situation can be seen here: https://www.oanda.com/currency/converter/
Conceptually, I was thinking of doing this:
Iterate through each div tag with class=List_item, and if I find one that has a display, count it. Then verify if it equals to '1'.
I tried using find_elements but can't seem to find attribute within each element in the array (is it because they aren't webdriver objects?).
If there's another better approach, it'd be really good to know and learn more as well. Appreciate any responses.
So, in this particular example, you could do
#driver.find_elements(:xpath, "//div[#class='currency_dropdown']/div[#id='scroll-innerBox-1']/div[not(#style='display: none;')]").size
This should return 1 when USD is typed into the search field.
Edit: I recommend getting ChroPath for your preferred browser (FF or Chrome). It helps significantly when testing xpath.

Google Spreadsheet xpath scraping

So I'm not a professional programmer, but I'm trying to scrape data off the Reuters homepage and import it into google spreadsheets.
I know that there have already been questions answerd about scraping from Reuters, however, that didn't help me.
I want data from this page: http://www.reuters.com/finance/stocks/financialHighlights?symbol=9983.T
specifically, if you scroll down, there's a lot of data on the company's financials, packed into tables. I need specific values out of the tables.
So naturally my question to you is, how can I get specific values out of the tables? For instance, I want the first value out of the line that's labelled "Net Profit Margin (TTM)". The value should be 7.30.
So I got the xpath by using google chrome developer tools, right-click on the element and select "copy xpath". Since I'm not a programmer I dont know any other way for arriving at a specific element from the tables.
I tried the following function in google spreadsheets:
=IMPORTXML(URL as written above,"//*[#id='content']/div[2]/div/div[2]/div[1]/div[13]/div[2]/table/tbody/tr[14]/td[2]")
but it returns
"#N/A - Error, imported content is empty"
What can I do to get the value?
The IMPORTXML() function of Google Sheets is known to be incredibly buggy and it is not surprising if people dig up real errors in it. Still, we don't know exactly why your original XPath expression does not work.
I want the first value out of the line that's labelled "Net Profit Margin (TTM)". The value should be 7.30.
The path expression you got from the developer tools heavily relies on positioning, and not at all on actual values.
If you can rely on the text content of the first cell in this row, use
=IMPORTXML("http://www.reuters.com/finance/stocks/financialHighlights?symbol=9983.T","//tr[contains(td[1],'Net Profit Margin (TTM)')]/td[2]")
which means
Select all tr elements where the text content of the first td child element contains "Net Profit MArgin (TTM)" and select the second td of that tr.
and the result will be
7.3

Selenium cannot find text box

I am trying to record my test on a major web based application in Firefox using Selenium IDE. There are some textboxes in one web page where I want to input some values. The problem is whenever I run the test to record it, the id of the textbox elements change in every session. I used xpath to locate the textboxes but the xpath just gives me the same element id. Can anyone tell me how do I go about locating textboxes?? I have also looked into the source code and nothing else is unique among the 4 textboxes except the id which changes on every run. Thanks
try to locate text box by name
Eg.text box name is To then script will be
<tr>
<td>type</td>
<td>name=to</td>
<td>testt</td>
</tr>

Selenium IDE - How to record xpath in the Firefox plugin

I'm having trouble with my xpaths in the Firefox plugin. I have three textboxes, the first one has ID=login and the rest has dynamically generated IDs. The first one works fine to write in the plugin, //input[#id='login'] but as soon as I try something more advanced, it cannot find anything. After reading plenty of forum posts, I've tried the XPather plugin to generate the xpath codes but the long html/css-filled strings don't work either. In some threads, people write "xpath=//..." and I tried that too, to no result.
Thankful for all help possible!
//M
The two tools I use are Firebug and XPath Checker, both Firefox add-ons.
In Firebug, select the HTML tab, right click on the element and select "Copy XPath"
For XPath Checker, right click on the element in the page and select "View XPath".
You can check this by pasting the result into the target field of Selenium IDE and then clicking the "Find" button. This tells what the Selenium result is going to be (much faster than having to run the test over and over!)
I've found that you have to "massage" the result somewhat e.g.
If you go to the Google home page and look for the "Images" link at the top left, the XPath Checker gives the image XPath as:
//id('gbar')/x:nobr/x:a1
This throws an error:
[error] locator not found: //id('gbar')/x:nobr/x:a1, error = Error: Invalid xpath 2: //id('gbar')/x:nobr/x:a1
If you remove the id e.g
//x:nobr/x:a1
you'll notice that the image has a green box around it showing thet the IDE has correctly parsed it.
Alright, I found some new information and it does work with writing "xpath=/" and the auto-generated codes from XPather. Although, the xpaths are very long and rely a lot on the HTML tags staying the same.
Does anyone have a better idea for xpath expressions (or css expressions) to write in order to enter information in the following three textbox elements:
TEXTBOX 1
Firebug: input type="text" tabindex="110"
name="6011300f91d9f186d1b7ab1a034827da"
id="input1"
Working xpath in Selenium, retrieved from XPather:
xpath=/html/body[#id='fire']/form[#id='loginForm']/div/div/div[#id='container']/div[#id='content']/div[#id='contentPadding']/div[1]/fieldset[1]/input[#id='input1']
TEXTBOX 2
input type="password" tabindex="110"
name="0de4766295b7a965fc4969da3df6824ba"
xpath=/html/body[#id='fire']/form[#id='loginForm']/div/div/div[#id='container']/div[#id='content']/div[#id='contentPadding']/div[1]/fieldset[2]/input
TEXTBOX 3
input type="password" tabindex="110" class="last" name="6c4e0fcde65e258c3dc7c508a1cc666a"
xpath=/html/body[#id='fire']/form[#id='loginForm']/div/div/div[#id='container']/div[#id='content']/div[#id='contentPadding']/div[1]/fieldset[3]/input
//M

Resources