Html 使用 WebDriver 单击 <div class > 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19260404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
click on a <div class >link using WebDriver
提问by Insane
I am not able to figure out how to click the link within div***
我不知道如何点击 div 中的链接***
Below is my page resource, xpath, css & my failure attempts. With each of the attempts, I received org.openqa.selenium.NoSuchElementException: no such element...
下面是我的页面资源、xpath、css 和我的失败尝试。每一次尝试,我都收到了org.openqa.selenium.NoSuchElementException: no such element...
Help?
帮助?
<form id="SearchAllCampaignsForm" action="/as/update.do" method="post" name="SearchAll">
<style>
<script>
<div class="body-content">
<input type="hidden" value="" name="campCookie">
<div id="container" class="Coptions">
<div class="containcamps">
<div id="dwrapper" class="dTables_wrapper" role="grid">
<div class="owrapper">
<div class="refresh">
<div class="addRow">
***<div class="AddContentBTN" title="Add New" rel="createCampaign">Add New</div>***
</div>
</form>
My attempts:
我的尝试:
@Test
public void addCamp(){
//WebElement link = driver.findElement(By.linkText("Add New"))
//driver.findElement(By.xpath("//div[@class='AddContentBTN']/rel[text()='createCampaign']")).click();
//driver.findElement(By.xpath("//a[@title = 'Add New']")).click();
//Actions builder = new Actions(driver);
//builder.moveToElement(addCamp).click().perform();
//driver.findElement(By.cssSelector("div.wrapper div.row-fluid form#SearchAllCampaignsForm div.body-content div.container div#dataTable_wrapper.dataTables_wrapper div.optionswrapper div.addRow div.AddContentBTN")).click();
}
xPath and CSS:
xPath 和 CSS:
/html/body/div[3]/div/form/div/div[2]/div/div/div[2]/div
html body div.wrapper div.row-fluid form#SearchAllCampaignsForm div.body-content div.container div#dataTable_wrapper.dataTables_wrapper div.optionswrapper div.addRow div.AddContentBTN
采纳答案by Ben Smith
回答by Daniel Gruici
Because I stumbled upon this while looking for my own solution, this will work as well. It is for C#
因为我在寻找自己的解决方案时偶然发现了这一点,所以这也能奏效。它适用于 C#
driver.FindElement(By.ClassName(classNameOfOuterDiv)).FindElement(By.ClassName(nameOfClickableDiv));