eclipse Selenium 2 WebDriver 无法找到链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7709185/
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
Selenium 2 WebDriver not able to find link
提问by crazyaboutliv
I saw the other questions regarding similar/same issues but they did not help me solve the problem :(. I log into the production site . say ( http://www.site.com/log) . I want to click on a link after that but Selenium is not able to find the link. The relevant HTML part is :
我看到了有关类似/相同问题的其他问题,但它们没有帮助我解决问题:(。我登录到生产站点。说(http://www.site.com/log)。我想单击之后的链接,但 Selenium 找不到链接。相关的 HTML 部分是:
<div style="display:none" id="managers">
<a class="projectManager" style="color:black"> Project Manager</a>
<a class="transportManager"> Transport Manager</a>
</div>
The java code is below:
Java代码如下:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
public class test {
private WebDriver driver;
private String baseUrl="";
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
//driver = new FirefoxDriver();
//driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
String chromeBinary = System.getProperty(" ");
if (chromeBinary == null || chromeBinary.equals("")) {
String os = System.getProperty("os.name").toLowerCase().substring(0, 3);
chromeBinary = "lib/chromedriver-" + os + (os.equals("win") ? ".exe" : "");
System.setProperty("webdriver.chrome.driver", chromeBinary);
}
driver=new ChromeDriver(chromeCapabilities);
driver.manage().timeouts().implicitlyWait(70,TimeUnit.SECONDS);
}
@Test
public void testEmployee() throws Exception {
driver.get("http://www.site.com/log");
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys("username");
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys("password");
driver.findElement(By.cssSelector("input[name=\"login\"]")).click();
Thread.sleep(10000);
driver.findElement(By.linkText(" Project Manager")).click();
driver.findElement(By.linkText("Sign Out")).click();
System.out.println("Test done");
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
Question: What is the error ? It gives an "element not found" exception
问题:错误是什么?它给出了“找不到元素”的异常
Thanks.
谢谢。
回答by Amit Horakeri
Try using the following..
尝试使用以下..
driver.findElement(By.partialLinkText(" Project Manager")).click();
driver.findElement(By.partialLinkText("Sign Out")).click();
Hope this works.
希望这有效。
回答by user3487861
My Understanding: I think here you are looking for the link "Project Manager".
我的理解:我认为您在这里寻找的是“项目经理”链接。
Step 1: Link Text Won't Support Always.So Go with CSS Selector
第 1 步:链接文本不会一直支持。所以使用 CSS 选择器
CSS Selector for Project Manager:css=.projectManager
项目经理的 CSS 选择器:css=.projectManager
Step 2:
第2步:
Perform the Click
执行点击
driver.findElement(By.cssSelector(".projectManager")).click();
AlterNative Methods to click
单击的替代方法
driver.findElement(By.className("projectManager")).click();
driver.findElement(By.partialLinkText("Project Manager")).click();
If you have a CSS class you should prefere this solution to finding an element by the link text as it is far more stable and can also be used on multi language UIs
如果你有一个 CSS 类,你应该更喜欢这个解决方案,而不是通过链接文本查找元素,因为它更稳定,也可以用于多语言 UI
Thanks!
谢谢!
回答by Diego C.
What about removing the blank in the link text string? You have
删除链接文本字符串中的空白怎么样?你有
driver.findElement(By.linkText(" Project Manager")).click();
try
尝试
driver.findElement(By.linkText("Project Manager")).click();
HTML doesn't count spaces.
HTML 不计算空格。