Return value from an HTML class/scope? - python-3.x

I have a webpage that I'm trying to return a value from, however I can't find the right way to grab it with Selenium.
Here's the relevant HTML part:
<table class="table table-striped">
<tbody>
<tr class="hidden-sm hidden-xs">
<th scope="row"><a style="cursor: pointer"
onClick="document.formShip.P_IMO.value='9526942';document.formShip.submit();">
9526942</a>
</th>
I'm trying to get 9526942.
I've tried:
imo = driver.find_element_by_xpath("//*[contains(text(), 'document.formShip.P_IMO.value')]")
and looked around here, but don't know what element this is. I tried looking for the class hidden-sm hidden-xs, to no avail:
imo = driver.find_element_by_class_name('hidden-sm hidden-xs')

if you want to get the text you need to use .text. The .text method can be used with a webelement which some text in that.
in your first example which you tried, you are passing a different parameter with text(). usually when you use text(), you need to pass the value which is there between closing and open tags (the text which you see on the screen)
you simply try this.
imo = driver.find_element_by_xpath(.//tr[#class='hidden-sm hidden-xs']).text

Related

Selecting a webpage radio-button

I am trying to do some webpage data extraction using VBA within excel. I have managed to automate the login process, which takes me to a new page with a search form. This form has an input field for 'Search Value' and a series of radio buttons that specify the variable to conduct the search on eg. ID number, Name. I am trying to automate filling in the input field and selecting one of the radio buttons and then submitting the form.
The code I used to login does not seem to work on the next page where I need to do the search. This is the HTML from the search page containing the input field:
<table width="100%">
<tr>
<td style="font-weight: bold; width:10%;" nowrap="nowrap">Search Value</td>
<td style="width: 15%; text-align: left;">
<input name="txtSearch" type="text" id="txtSearch" onkeyup="return txtSearch_onkeyup()" style="background-color:White;border-color:#222244;border-width:1px;border-style:Solid;height:20px;width:125px;" />
Which I try to fill in using:
ieDoc.Document.getElementsByName("txtSearch")(0).Value = "Test String"
Or:
ieDoc.Document.all.txtSearch.Value = "Test String"
Or
ieDoc.Document.getElementByID("txtSearch").Value = "Test String"
...all giving me the same Object defined error.
I have confirmed that the ieDoc is referencing the correct page after the login (by checking the title in the immediate window), and tried to ensure it is not a timing issue with page-loading.
I know the HTML methods in vba tend to be temperamental but I've run out of ideas. Any other way to access the input field on this page?
Success!
I was trying to reference an element on a sub-frame within the IE object, which I accessed with:
ieDoc.Document.frames(0).Document.all.txtSearch.Value = "Hello!"

Loop in table with Watir

Stuck with watir trying to create a loop to click in all the links included in a table. Currently the table has this format:
<table id="test">
<tbody><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Link</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>http://facebook.com</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>http://google.com</td>
</tr>
</tbody></table>
And my current attempt looks like:
browser.table(:id => "test").rows do |tr|
tr.each do |td|
td.links.click
end
end
This code above does nothing in the browser & neither returns something in the terminal (no errors, no outputs).
Also tried a different approach using columns:
columns = browser.table(:id => "test").strings.transpose
browser.columns.each do |t|
t.click
browser.back
end
That outputs this error: jsfiddle.rb:24:in <main>': undefined methodcolumns' for # (NoMethodError)
The newer versions of Watir, version 6.0, automatically attempts to relocate stale elements. As a result, as long as the initial page does not change each time you reload it, you no longer need to save some sort of reference data before clicking the links.
It simply becomes:
my_table = browser.table(:id, 'test')
my_table.links.each do |link|
link.click
browser.back
end
This should work to click on each link in the table:
my_table = browser.table(:id, 'test')
table_links = my_table.links.map(&:text)
table_links.each do |link_text|
my_table.link(:text, link_text).click
browser.back
end
Largely based on Justin Ko's answer here.

How to find two different class names using find_elements_by_css_selector

I can't seem to find an answer for this on the net.
Here's a snippet of html code:
<td>
<div class="low-fare-day active"></div>
<div class="low-prices"></div>
</td>
<td>
<div class="low-fare-day"></div>
<div class="low-prices1"></div>
</td>
Below is my code:
I want to find the two classes low-fare-day and low-fare-day.active using css_selector, but couldn't get it working. Can anyone solve this puzzle for me?
fromdata = driver.find_elements_by_css_selector('div.low-fare-day','div.low-fare-day.active')
or
fromdata = driver.find_elements_by_css_selector('div.low-fare-day' | 'div.low-fare-day.active')
Try it:
driver.find_elements_by_css_selector('div[class*=low-fare-day]')
Explanation:
div[class*=low-fare-day] -> means you're looking for a div
div[class*=low-fare-day] -> you're selecting the class value inside the div selected before to compare values
*= after class means you will cath all that is equals or that contains the next value
div[class*=low-fare-day] -> the value to compare if the div's class contains it

Adding new xElement after ALL found Descendants

I have an xDocument with multiple various xElements.
I can successfully find a specific xElement by searching via it's xAttributes & then Add a new xElement after it using the code below:
xDocument.Descendants("td").LastOrDefault(e => ((string)e.Attribute("ID")) == "3").Add(new XElement("b", "Just a test."));
The problem is that I wish to Add this new xElement after all found instances of the Descendants, not just LastOrDefault or FirstOrDefault.
My xDocument is created dynamically & there is no way before hand to know how many 'td' xElements with 'ID' = '3' that there are going to be.
Any help would be appreciated.
Thanks
ADDED CODE AS REQUESTED
<html> .... etc....
<body>
<table>
<tr>
<td>Image</td>
<td>Description</td>
<td>Date</td>
</tr>
<tr>
<td ID="1">*.jpg</td>
<td ID="2">some image</td>
<td ID="3">01/01/1901</td> <--CHANGING THIS PART OF CODE-->
<--THIS TABLE ROW REPEATS AN UNDETERMINED NUMBER
OF TIMES RELATING TO THE NUMBER OF FILES CONTAINED IN WHATEVER DIRECTORY IS BEING SEARCHED USING A FOREACH LOOP IN ANOTHER PART OF
THE CODE-->
</tr>
</table>
</body>
</html>
So I am trying to add a tag between the <td> with ID = 3.
This <b> tag also contains a string variable i.e.
new xElement("b", DateTaken)
& needs to be created at runtime and not hard coded as it relates to each loaded image at the start of the table row.
So I am trying to add this <b> tag to every occurrence of <td> with ID=3 & not just the first or the last.
Hope this extra info helps.

Selenium-IDE: How to simulate non-printable keys (ENTER, ESC, Backspace)?

What is the exact HTML code to simulate ENTER, ESC, BACKSPACE and DOWN in Selenium IDE 1.3.0?
typeKeys didn't work nor did this:
<tr>
<td>keyDown</td>
<td>id=zc_0_4_3-real</td>
<td>10</td>
</tr>
<tr>
<td>keyUp</td>
<td>id=zc_0_4_3-real</td>
<td>10</td>
</tr>
<tr>
<td>keyPress</td>
<td>id=zc_0_4_3-real</td>
<td>10</td>
</tr>
None of the solutions above helped me, however, the special keys described here this did the trick:
http://blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-1/
sendKeys | id=search | ${KEY_ENTER}
Special keys - like normal keys, only a bit special. :)
For example to submit a form by pressing enter, the only one I can figure out is:
Command: keyPressAndWait
Target: id=q [depends on your form of course]
Value: \\13 [for enter - any ascii value can go here]
So it looks like this:
<tr>
<td>keyPressAndWait</td>
<td>id=q</td>
<td>\13</td>
</tr>
Hope it helps
Paul
Update:
keyPressAndWait is deprecated
Now you can use:
Command: sendKeys,
Target: id=<your id>,
Value: <your letter in utf8 and not ascii anymore>
For non-printable keys you can have a look at this page:
http://www.testingdiaries.com/selenium-ide-keypress-events/
you can use ${KEY_ENTER} and for other keys as the same as ${KEY_F8},${KEY_ESC}.. etc
Here is a blog post with more details.
For the newer versions of Firefox (22 & 23) the typeKeys command won't work in the Selenium IDE. It's deprecated. You have to use sendKeys.
command = sendKeys
target = css=.someclass
value = ${KEY_ENTER}
If you want to combine text with special keys you can do something like:
command = sendKeys
target = css=.someclass
value = demo${KEY_ENTER}
These methods doesn't work with the TAB key.
To simulate the TAB key pressed we need to use the command fireEvent like this
Clear text field using Ctrl+A and Del (for Selenium IDE):
<tr>
<td>keyDown</td>
<td>id=your text field id</td>
<td>\17</td>
<tr>
<td>keyPress</td>
<td>id=your text field id</td>
<td>\65</td>
<tr>
<td>keyUp</td>
<td>id=your text field id</td>
<td>\17</td>
<tr>
<td>keyPress</td>
<td>id=your text field id</td>
<td>\127</td>
You can use code 13 for enter key, code 9 for tab key, code 40 for down key, 8 for backspace key
The Best Answer to the qs how to Record the Enter Key Through Selenium IDE
<tr>
<td>keyDown</td>
<td>id=txtFilterContentUnit</td>
<td>\13 </td>
</tr>
Its Working i tried that on Selenium IDE here. replace txtFilterContentUnit with your text box name.
hope u can do it -Abhijeet

Resources