我有一个生成动态复选框的表格,如下所示:
<div style="background: #fff;">
<div class="modal-body">
<table class="table no-margin">
<thead>
<tr>
<th>Select</th>
<th>Group Name</th>
<th>Contacts</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='1' data-grpname='Newsletter' />
</label>
</div>
</td>
<td>Newsletter</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='2' data-grpname='Website Leads Buyer' />
</label>
</div>
</td>
<td>Website Leads Buyer</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='3' data-grpname='Website Leads Seller' />
</label>
</div>
</td>
<td>Website Leads Seller</td>
<td>2</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='4' data-grpname='Website Leads Investor' />
</label>
</div>
</td>
<td>Website Leads Investor</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='5' data-grpname='Website Leads' />
</label>
</div>
</td>
<td>Website Leads</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='7980' data-grpname='NewGroup1' />
</label>
</div>
</td>
<td>NewGroup1</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='7996' data-grpname='My Group' />
</label>
</div>
</td>
<td>My Group</td>
<td>0</td>
</tr>
<tr>
<td>
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" class="datacontact" onchange="SelectContacts(this);" data-listid='9136' data-grpname='Email Functionality ' />
</label>
</div>
</td>
<td>Email Functionality </td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-flat btn-default-dark ink-reaction">OK</button>
<button type="button" data-dismiss="modal" class="btn btn-flat btn-default-dark ink-reaction">Cancel</button>
</div>
</div>
</div>
</div>
</div>
通过下面提到的代码,我获得了名为“网站潜在销售商”复选框的价值
package TestScripts;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement;
//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
public class EmailClient_VerifyEmail {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:/geckodriver/geckodriver-v0.21.0-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://cp.iagentsolutions.com/login.aspx");
//enter email id
driver.findElement(By.cssSelector("#txtuserName")).sendKeys("abdul123");
//enter password
driver.findElement(By.cssSelector("#txtPassword")).sendKeys("admin123");
// click on login
driver.findElement(By.cssSelector("#LoginSnd")).click();
System.out.println("User Login Successfully from CP");
//hover on menu
//driver.findElement(By.xpath("html body.menubar-hoverable.header-fixed.menubar-visible form#form1 div#base div#menubar.menubar-inverse.animate div.nano.has-scrollbar div.nano-content div.menubar-scroll-panel ul#main-menu.gui-controls"))
Actions action = new Actions(driver);
//WebElement element =
action.moveToElement(driver.findElement(By.cssSelector(".menubar-foot-panel"))).perform();//("html body.menubar-hoverable.header-fixed.menubar-visible form#form1 div#base div#menubar.menubar-inverse.animate div.nano.has-scrollbar div.nano-content div.menubar-scroll-panel ul#main-menu.gui-controls")));
//click on Email Client menu for open sub-menus
driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[2]/div[1]/div/ul/li[7]/a/span")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
// click on Create Email
driver.findElement(By.xpath("/html/body/form/div[3]/div[2]/div[2]/div[1]/div/ul/li[7]/ul/li[1]/a/span")).click();
//Enter draft name
driver.findElement(By.name("ctl00$body$txtDraftName")).sendKeys("Try to send without verify email");
//Click on Next button
driver.findElement(By.name("ctl00$body$btnStep1Next")).click();
//Email Settings
// Enter subject
driver.findElement(By.id("body_txtSubject")).sendKeys("Try to send without verify email");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
//Click on Add / Edit to open Contact List
driver.findElement(By.linkText("Add / Edit")).click();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElements(By.xpath("//input[@type='checkbox' and @class='datacontact']")).forEach(elelemnt -> {
elelemnt.click();
});
}
}
但是此代码仅适用于获取复选框标题,而不是单击复选框。
答案 0 :(得分:1)
我已经对其进行了测试,并且工作正常: 使用以下代码:
driver.findElements(By.xpath("//input[@type='checkbox' and @class='datacontact']")).forEach(element -> {
if (element.getAttribute("data-grpname").trim().equals("Website Leads Seller")) {
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
}
});