问题
I have a page with multiple rows that each have a link, "Add Tenant Info" but I can't seem to click it in a way that would be unique to each row. I have been able to select take actions with
driver.findElement(By.cssSelector("a[class*='sf-with-ul']"));
Here is the HTML of the section:
<tr class="false sub_row data_row highlight" id="spreadsheet_row_7117098" style="">
<td style="background-color: rgb(232, 245, 209);">Tenant 1 Name</td>
<td style="background-color: rgb(232, 245, 209);"><a class="action-link">Add Tenant Info</a></td>
<td style="text-align: center; padding-left: 10px; padding-right: 10px; background-color: rgb(232, 245, 209);"> <span></span> </td>
<td style="background-color: rgb(232, 245, 209);"><a class="action-link">Add Lease</a></td>
<td style="width: 84px; padding: 0px; background-color: rgb(232, 245, 209);">
<div style="">
<ul class="sf-menu sf-js-enabled sf-arrows">
<li><a class="sf-with-ul">Take Actions</a>
<ul>
<li><a class="action-link">Add Tenant Info</a></li>
<li><a class="action-link">Tenant Screening Center</a></li>
<li><a class="action-link">Delete Tenant</a></li>
</ul>
</li>
</ul>
</div>
</td>
</tr>
I am using chrome driver and selenium 3.8.1
Edit:
Navigate to this site simplifyem.com
Log in with
user: stackoverflow@gmail.com
pass: password
Then navigate to http://www.simplifyem.com/tenant_management/overview
I want to click on the "Add Tenant Info" but I need to iterate over all of them and click each one.
回答1:
Have you tried using a different xpath? You can try doing something like:
driver.findElement(By.xpath("//li[@class='sf-with-ul']"));
Chrome driver: 2.33.5
Selenium: 3.4
Chrome browser 62.0
回答2:
You can just use By.linkText("Add Tenant Info"). I tried it and it worked just fine.
List<WebElement> links = driver.findElements(By.linkText("Add Tenant Info"));
System.out.println(links.size());
回答3:
How about thisc:
List<WebElement> links = driver.findElements(By.xpath("//td/a[@class='action-link' and contains(text(),'Add Tenant Info')]"));
for (int x=0; x<links.size; x++) {
driver.findElement(By.xpath("(//td/a[@class='action-link' and contains(text(),'Add Tenant Info')])[x]")).click();
//do other stuff
//go back to original page
}
Remember that you have to go back to the original page to click on the links. Also, you can't just iterate over the list of webelements you got before because by clicking on the link and navigating to a different page, then you are basically expiring the web elements you got. And thus when you come back to the original page, and try clicking on the web elements in the list, it will say that the element has expired.
Edited: Just checked the web app you provided
回答4:
As per the HTML of the URL in your question there are multiple Add Tenant Info links present. To iterate over all of them and click each one you can use the following code block :
List<WebElement> Add_Tenant_Info = driver.findElements(By.xpath("//table[@class='table_view']//tbody//tr[@class='false sub_row data_row' or @class='false sub_row data_row highlight']//a[contains(.,'Add Tenant Info')]"));
for(int i=0; i<Add_Tenant_Info.size();i++)
{
//Click on any Add Tenant Info link through index "i"
Add_Tenant_Info.get(i).click();
}
来源:https://stackoverflow.com/questions/48103210/cant-click-action-link