gpt4 book ai didi

java - Selenium 2 WebDriver 找不到链接

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:32:55 25 4
gpt4 key购买 nike

我看到了关于类似/相同问题的其他问题,但它们并没有帮助我解决问题:(。我登录到生产站点。说(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>

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;
}
}
}

问题:错误是什么?它给出了一个“找不到元素”的异常

谢谢。

最佳答案

尝试使用以下..

driver.findElement(By.partialLinkText(" Project Manager")).click();
driver.findElement(By.partialLinkText("Sign Out")).click();

希望这能奏效。

关于java - Selenium 2 WebDriver 找不到链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7709185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com