开发者

selenium: clicking on first link of the results page

I am testing a page similar to Google search where you would enter the search terms in the provided text box, and click Search, and the next page returns a bunch of results matching your search term. These results are all links to documents (doc files), and are random depending on your searched text. I am having a p开发者_如何学Croblem clicking on the first results link. I record the steps using selenium IDE, but when I run the tests, it fails on the point where it has to click on the first results link. The error I'm getting is:

Selenium.SeleniumException: ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window.  The error message is: Permission denied

I have tried this two ways:

selenium.Click("xpath=//html/body/form/div[2]/div[3]/div[3]/table/tbody/tr/td[2]/span/a/");
selenium.Click("xpath=//span[contains(@class,'ResultList_Title_Link')][1]/a");

When I right-click on the first results link, and do a inspect element, I see:

<w_lit_documenttitle wid="82e0-9888a350e66b">MEMORANDUM OF <span style="background-color:#FFFF66;color:#333333;font-weight:bold" name="wlCitedDoc" id="wlCitedDoc">LAW</span> COMPLAINT</w_lit_documenttitle>

The Xpath for the above is:

/html/body/form/div[2]/div[3]/div[3]/table/tbody/tr/td[2]/span/a/w_lit_documenttitle


i believe you have to do this in dynamic way as you are saying clicking on FIRST LINK.

after the results page loaded with all the results as a links, // if the id's are defined for every link you are going to click

String[] links = selenium.getAllLinks();

for(String link : links)

selenium.click(link);

//if no id's are defined

selenium.getEval("window.document.getElementsByTagName('a')[0].click()"); //this is to click the first link

i hope this is what you are looking for


Instead of passing in an xpath locator in your Click method, could you find the link by id or another locator? It would be a lot less complex and a lot more readable if you have to change it later.


Agree with @Ken Pespisa. I'm partial towards using CSS as an identifier. Extract the HTML (using Firebug) for 3-4 links returned by the search, and then identify some common attribute. You can then construct the CSS identifier using that attribute. If you need to simply click on the first link, a generic attribute will do. For a specific link, you will have to first assert it's presence and then click on it.

Here's a CSS identifier example using your sample HTML:

selenium.click("css=span#wlCitedDoc[name=wlCitedDoc]");


Since the error message is "permission denied", you should probably try to find out which element really defines your link. Using the click method of Selenium, you can click on clickable things such as <a> elements and buttons, but you have to specify them and not some portion of text within the element.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜