简体   繁体   中英

Locating an Element in Selenium

I am attempting to locate a link using Selenium in java. I want to use the web driver to click on the link. The element is a number that is a link to another page. This is the section of html containing the elements I would like to locate:

<tr class="DataGridPagerStyle">
<td colspan="5">
<span>1</span>
<a href="javascript:__doPostBack('ctl00$ctl62$dgPersonSearchResults$ctl19$ctl01','')">2</a>
<a href="javascript:__doPostBack('ctl00$ctl62$dgPersonSearchResults$ctl19$ctl02','')">3</a>
<a href="javascript:__doPostBack('ctl00$ctl62$dgPersonSearchResults$ctl19$ctl03','')">4</a>
</td>
</tr>

I would like to flip through the pages, therefore I need to locate the "a href" elements. There are sometimes a different number of pages. I attempted to locate and click on these elements using the following java code:

String href = doc.select("tr.DataGridPagerStyle").first().select("a:contains(" + i + ")").first().attr("href");
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href=" + href + "]")));
element.click();

The href string does contain the right value of the href attribute for a given value of i, however I get this error when running the code:

The given selector a[href=javascript:__doPostBack('ctl00$ctl62$dgPersonSearchResults$ctl19$ctl01','')]
is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal selector was specified

Why is this happening and what is the best way to select these elements?

You can find the links by linkText() :

link = doc.select("tr.DataGridPagerStyle").first().findElement(By.linkText(i))
link.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM