Jimit Shah
Jimit Shah

Reputation: 1

Unable to click on link using Link text/Partial Link text using ruby

I am new here and even to Ruby and Selenium. I am trying to click a link on web page which has following code:

<a> target="mainFrame" href="dynamic_Utility_Index.htm">Dynamic Utilities</a>

So basically I want to click on this dynamic utilities. The script which I have written so far is:

    require 'selenium-webdriver'
    require 'win32ole'

    driver = Selenium::WebDriver.for:firefox
    driver.manage().window().maximize();

    driver.navigate.to 'xyz.com'

    wait = Selenium::WebDriver::Wait.new(:timeout =>10) # seconds

    #Click on Dynamic Utilities
    wait.until{driver.find_element(:link_text,'dynamic_Utility_Index.htm').click}
    puts "Clicked"

I have even used link, partial_link_text in place of link_text but keep getting the following error

(Unable to locate element: {"method":"link text","selector":"dynamic"}) (Selenium::WebDriver::Error::TimeOutError)

I am using Ruby and Selenium Web driver.

Upvotes: 0

Views: 1881

Answers (2)

Juan Castelli
Juan Castelli

Reputation: 185

Did you try with:

wait.until{driver.find_element(:link_text,'DYNAMIC UTLITIES').click}

?? Sometimes I had to use the text link by capitalized.

Another option could be:

driver.find_element(:xpath, "//a[contains(@target, 'mainFrame')]").click

Hope works for you :D

Upvotes: 1

Andersson
Andersson

Reputation: 52665

The item you consider to be a link text (dynamic_Utility_Index.htm) actually just a value of href attribute. Actual link text is "Dynamic Utilities". Also you can use xpath locator //a[@href="dynamic_Utility_Index.htm"] instead of search by link text:

wait.until{driver.find_element(:xpath,'//a[@href="dynamic_Utility_Index.htm"]').click}

Upvotes: 0

Related Questions