Get specific nested table cell using Ruby Watir - html

I'm writing a Watir program to extract a specific number from a dynamically-generated table. The number is always in the second column of the fourth-to-last row in the table, but I'm getting the following error when trying to access the general table element:
Watir::Table: located: false; {:id=>"tContenido", :tag_name=>"table"}
Code
require 'rubygems'
require 'watir'
browser = Watir::Browser.new :firefox
$log = Logger.new('logs\test.log')
$log.info("TEST")
$log.info browser.table(:id, 'tContenido')
HTML Structure of the target page (1)
HTML Structure of the target page (2)
Any help would be greatly appreciated, thanks in advance!

I think this is the one you are looking for:
browser.table(id: 'tContenido').tr(class: 'tr_gris', index -1).td(index: 1).text
Alternately, if you are having problems with a table, you can just put everything into an array of Hashes:
browser.table(id: 'tContenido').hashes

You can write the following WATIR code
puts b.table(id: 'tContenido').trs[-4].tds[2].text

Related

How to parse Table from Wikipedia using htmltab package?

All,
I am trying to parse 1 table located here https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population. And I would like to use htmltab package to achieve this task. Currently my code looks like following. However I am getting below Error. I tried passing "Rank", "% of world population " in which function, but still received an error. I am not sure, what could be wrong ?
Please Note: I am new to R and Webscraping, if you could provide explanation of the code, that will be great help.
url3 <- "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population#Sovereign_states_and_dependencies_by_population"
list_of_countries<- htmltab(doc = url3, which = "//th[text() = 'Country(or dependent territory)']/ancestor::table")
Error: Couldn't find the table. Try passing (a different) information to the which argument.
This is an XPath problem not an R problem. If you inspect the HTML of that table the relevant header is
<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">
Country<br><small>(or dependent territory)</small>
</th>
So text() on this is just "Country".
For example this could work (this is not the only option, you will just have to try out various xpath selectors to see).
htmltab(doc = url3, which = "//th[text() = 'Country']/ancestor::table")
Alternatively it's the first table on the page, so you could try which=1 instead.
(NB in Chrome you can do $x("//th[text() = 'Country']") and so on in the developer console to try these things out, and no doubt in other browsers also)

Type conversion error in VB when accessing a HTML table

I have a HTML table where I am allowing dynamic adding/deleting rows (that contain text-input's in the cells) using some JavaScript. I am not using ASP.NET TextBox controls, just traditional HTML, as I don't think rows containing ASP.NET controls can be added/deleted without a postback.
When the user clicks an ASP:Button, I am using VB to loop through the table rows in server-code, and ultimately use LINQ to write to a database. I have "Imports System.Web.UI.HtmlControls" in the file.
The code where I am getting problems is:
Dim name As HtmlInputText
name = row.Cells(0).Controls(0)
The error is: "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.HtmlControls.HtmlInputText'." On the 2nd line above.
There is only one input control in each cell, so I am assuming I can use "Controls(0)" to access it. I have read solutions that use "FindControl", but I don't think this works with standard HTML "inputs", but also, as the rows are dynamically added/deleted, it's near-impossible to know the "ID" to search for.
Any ideas? Cheers.
Figured out the problem. Just needed to add runat="server" to the input tags. Thanks every one for the replies.

How to code Regular Expression with an IF ELSE function

I am trying to build a scraper to extract key metrics from a website. One of the metrics is to find the Model number of the products on the website. I am using Outwit as the base program but I'm now stuck when it comes to some exceptions in the sites source code.
Here is an example of the source code:
var zx_description = "Test Dress<br/><br/>Model: Nice01j<br/>
Where the information I am looking to extract is: Nice01j
The issue is that for some products the word Modell is spelled Model and also that the end of the actual model name/number does not always end with a row break but in some cases the code might look like this:
var zx_description = "Test Dress<br/><br/>Model: Nice01j";
I have managed to create the RegEx before the Modell number as below:
/var zx_description[\s\S]+?Modell:/
So now Im looking to alter it so that it also takes in consideration that the spelling might be Model with just one "l".
Also the second part is to create a RegEx for the capturing of te info after the actual Model name which in should be something like:
IF: < br comes before "; then < br ELSE ";
Is this possible to state in a Regular Expression and if so how would I do that?
Based on your use of [\s\S] it looks to me like you need to run through a regular expression tutorial. For your question, specifically focus on optional items and capturing groups.
http://www.regular-expressions.info/tutorial.html

How to get rid of ElementNotFound error in Capybara (JRuby)

I'm new to capybara and jruby. As one of my practice, i used way2sms site. After clicking Send SMS, the control was not able to find the element to enter the phone number.
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
include Capybara::DSL
Capybara.run_server = false
Selenium::WebDriver::Firefox.path = "C:/Program Files/Mozilla Firefox/firefox.exe"
Capybara.default_driver = :selenium
visit "http://www.way2sms.com"
page.find(:xpath, '/html/body/form/div/div/div[2]/div/div/div[2]/a').click
fill_in('username',:with=>"username")
fill_in('password',:with=>"password")
click_button('button')
page.find(:xpath, '//*[#id="quickclose1"]').click
page.find(:xpath, '//*[#id="quicksms"]').click
page.fill_in('MobNo',:with=>"mobile number")
page.fill_in('textArea',:with=>"Some message5")
page.find(:xpath, '//*[#id="Send"]').click
click_button('Send SMS')
when i run this, it shows the following error,
Capybara::ElementNotFound: cannot fill in, no text field, text area or password
field with id, name, or label 'MobNo' found.
Anyone help me to clear this problem...
Without any more information my guess is that one of these causes the field for mobile number to appear maybe through ajax?
page.find(:xpath, '//*[#id="quickclose1"]').click
page.find(:xpath, '//*[#id="quicksms"]').click
Do you run your scenario with #javascript? That would be what I would try first.
Since the default driver is rack_test and that can not handle any javascript. And to get capybara to use javascript driver which is selenium, by default, you need to tag your scenario in cucumber with #javascript. Or with :js => true if you're using RSpec. Read about it in the capybara readme here
The root of the problem is that there is no input with id 'MobNo'. Are you sure that it is the correct id for the input field?
You can also see what the webpage looks like too cucumber/capybara by adding the step "Then show me the page" before the step that fails.

Edit mySQL record inside HTML Table via double click - Is it possible?

I have an HTML TABLE that displays my records from mySQL.
I need to edit various records (rows). Is it possible to click on a table row and edit the values in the row inside the HTML table?
I was wondering if there are any PHP scripts out there to guide me?
Yes, it's possible. While I am very convinced PHP is involved, Javascript is involved also.
I think you can have something like this:
1.Click once, javascript increases a counter variable
2. Click twice, javascript checks if var = 2 after increasing, resets counter, and changes innerHTML of the td to something like this:
form action ='yourphpscript.php' method = 'getorpost'>
.....What you want to change....
</form>
3. So then it sends your values to the PHP script, PHP script changes values in database, and you get redirected back to the main page. Or, better yet, use AJAX.
Hope this helped :)