开发者

How to set the URL of a link to a variable using WATIR

Using WATIR and Excel, I'd like to take the first row of a table in Excel, visit the URL, then set the 12th link on the page as a variable in the cell next to the cell with the URL, then go to the next line and repeat.

I'm stuck on getting the URL of the 12th link on the page to set as a variable that I can feed into the next cell in Excel. Here's what I have and it's not working.

worksheet = workbook.开发者_JAVA百科WorkSheets(1) # get first workbook


#declare test site
test_site = worksheet.Range("a2").text

#open ie
ie = Watir::IE.new

#go to test_site
ie.goto test_site

#find primlink
ie.link(:index, 12).text = "primlink"

puts primlink

Any ideas?


If you want to get URL from the 12th link, why are you using text method? Use href instead text.

#find primlink
primlink = ie.link(:index, 12).href

puts primlink


I didn't want it to click the link, I wanted it to return the URL without visiting the page, but this is what I ended up going with:

#declare test site
test_site = worksheet.Range("a2").text

#open ie
ie = Watir::IE.new

#go to test_site
ie.goto test_site

#find primlink
ie.link(:index, 12).click
#define primlink
primlink = ie.url() 
#goback
ie.back()
#find seclink
ie.link(:index, 14).click
#define seclink
seclink = ie.url()

#set primlink and seclink
worksheet.Range("b2").value = primlink
worksheet.Range("c2").value = seclink

Now I just have to figure out how to turn the cell references into relative references and iterate through an entire list or urls, but that's a whole other thread...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜