2013-10-15 2 views
0

Hi I'm trying to select an edit button and I am having difficulty selecting it.Ruby Watir - 참조 용으로 <a onclick="new Ajax.Request

<td> 
    <a onclick="new Ajax.Request('/media/remote/edit_source/3', {asynchronous:true, evalScripts:true}); return false;" href="#"> 
    <img title="Edit" src="/media/images/edit.gif?1258500617" alt="Edit"> 
</a> 

I have the number at the end of ('/media/remote/edit_source/3') the which changes and I have stored it in @rep_id variable.

I can't use xpath because the table changes often. Any suggestions? Any help is greatly appreciated. Below is what I have tried and fails. I am fairly new to watir and love it, but occasionally I run into things like this and get stumped.

browser.a(:text, "/media/remote/edit_source/#{@rep_id}").when_present.click 

답변

1

라인 :

browser.a(:text, "/media/remote/edit_source/#{@rep_id}").when_present.click 

실패 이유는

  1. 당신이 찾고있는 내용 로케이터는 문자열을 전달
  2. 온 클릭 속성 (대신 텍스트)에 두 번째 매개 변수. 즉, 정확히 일치하는 것을 찾고 있음을 의미합니다. 텍스트/속성의 일부만 사용한다고 가정하면 정규식을 사용해야합니다.

watir-webdriver를 사용하는 경우 : onclick 속성을 사용하여 요소를 찾을 수 있습니다. : onclick 속성과 부분적으로 일치하는 정규 표현식을 사용할 수 있습니다.

browser.link(:onclick => /#{Regexp.escape("/media/remote/edit_source/#{@rep_id}")}/).when_present.click 

또한 watir-classic (IE 테스트 용)을 사용하는 경우 위의 기능이 작동하지 않습니다. 대신 링크의 html을 확인할 수 있습니다. html을 확인하는 것은 watir-webdriver에서도 작동하지만 onclick을 사용하는 것보다 덜 강력합니다.

browser.link(:html => /#{Regexp.escape("/media/remote/edit_source/#{@rep_id}")}/).when_present.click 
1

From your example, it looks like you are using the URL from the onclick event handler as a :text locator, which I'd expect to fail unless that text does exist.

You could potentially click on the img. Examples:

browser.image(:title, "Edit").click 
browser.image(:src, "/media/images/edit.gif?1258500617").click 
browser.image(:src, /edit\.gif\?\d{10}/).click     # regex the src 

Otherwise, you might need to use the fire_event method to trigger the event handler, which looks like this:

browser.link(:id, "foo").fire_event "onclick" 

These are the links to the fire_event docs for watirwatir-webdriver을 선택하는 방법.

+0

의견을 보내 주셔서 감사합니다. 정규식은 완벽하게 작동했습니다. 정말 고맙습니다. 나는 정말로 붙어 있었다. –