当与Java驱动程序一起使用时,如何在Selenium中为链接触发click事件?
我用了当与Java驱动程序一起使用时,如何在Selenium中为链接触发click事件?,java,eclipse,selenium,selenium-rc,Java,Eclipse,Selenium,Selenium Rc,我用了 selenium.click("link=Sign In"); 我试着用 selenium.click(".//*[@id='global-signin']/a"); 两者都没有给我结果 我得到的错误如下:- Element Link=“登录”未找到错误 代码: 故障跟踪: com.thoughtworks.selenium.SeleniumException: ERROR: Element link=Sign In not found at com.thoughtwork
selenium.click("link=Sign In");
我试着用
selenium.click(".//*[@id='global-signin']/a");
两者都没有给我结果
我得到的错误如下:-
Element Link=“登录”未找到错误
代码:
故障跟踪:
com.thoughtworks.selenium.SeleniumException: ERROR: Element link=Sign
In not found at
com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
at
com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
at
com.thoughtworks.selenium.DefaultSelenium.click(DefaultSelenium.java:193)
at
package1_IdentifyPageOpened.PublicClass3.testTt4(PublicClass3.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at
org.junit.runners.ParentRunner.run(ParentRunner.java:300) at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
包含此选项的),可在每次搜索中使用
在这种情况下,通过以下方式进行操作要容易得多:
我以前也遇到过同样的问题,selenium中经常出现“Element not found”(元素未找到)问题。该问题的可能原因如下:
I.innerHTML = '<a href="' + D + "...a loong piece of URL..." + H + '">Sign In</a>';
当您执行“click element”时,但是该元素没有显示在页面中,可能是在加载过程中,selenium会抛出一个异常:element not found
元素的标识错误,可能是拼写错误或selenium不支持标识方式
我访问了你测试的网站
当您导出为Java时,SeleniumIDE
记录的测试用例总是不起作用
这是我的密码:
公共类网站扩展了SeleneseTestBase{
@预处理法
public void setUp()引发异常{
selenium=new DefaultSelenium(“本地主机”,4444,“*chrome”,”http://www.washingtonpost.com/");
selenium.start();
}
@试验
public void testWebsite()引发异常{
硒。开(“/”);
等待(“链接=登录”);
waitSecomd(3);
selenium.单击(“链接=登录”);
selenium.waitForPageToLoad(“60000”);
selenium.type(“name=MemberName”adasf@gmail.com");
selenium.type(“name=Password”、“adfasd”);
selenium.click(“name=submit”);
selenium.waitForPageToLoad(“60000”);
}
@后置法
public void tearDown()引发异常{
seenium.stop();
}
公共void waitFor(字符串定位器)引发异常{
for(int second=0;second++){
如果(秒>=60)
失败(“超时”);
试一试{
if(硒可见(定位器))
打破
}捕获(例外e){}
睡眠(1000);
}
}
public void waitSecomd(int n)引发异常{
for(int second=0;second++){
如果(秒>=60)
失败(“超时”);
试一试{
如果(秒>n-1)
打破
}捕获(例外e){}
睡眠(1000);
}
}
}
这很困难-我遇到的问题在第二页-Selenium无法在表单中输入任何数据,除非我首先选择了目标定位器。以下是我的解决方案:
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final int PAUSE = 10;
@Test
public void testWebsiteTwo() throws Exception {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(PAUSE, TimeUnit.SECONDS);
driver.get("http://www.washingtonpost.com/");
WebElement signin = driver.findElement(By
.cssSelector("li#global-signin a"));
signin.click();
// this line is crucial so that Selenium can enter the username / password
WebDriver.TargetLocator locator = driver.switchTo();
locator.frame(0);
WebElement name = driver.findElement(By.cssSelector("input#username"));
name.sendKeys(USERNAME);
WebElement password = driver.findElement(By.cssSelector("input#password"));
password.sendKeys(PASSWORD);
WebElement form = driver.findElement(By.cssSelector("form[name=form]"));
form.submit();
Thread.sleep(5000);
driver.close();
}
}
你也可以发布HTML吗?链接不是隐藏在frame
或iframe
元素中吗?在这种情况下,您需要在单击该链接之前执行以下操作。若并没有,请给我们一个最小的(但可复制的)HTML片段,以便我们可以帮助你们。@a.J已经包含了代码…请。现在看,并提供您的评论和建议…@Slanec pls。现在看一下,请提供您的建议。我以前用id=global signin检查过,还有,等待时间,它对我不起作用,最初…它从您这边起起作用吗?编辑。很多你应该认真考虑切换到WebDriver。但是硒溶液现在对我也有效。请随时询问是否有任何不清楚的地方。感谢您让它变得更简单…第一种方法,您提供的via exception不起作用…它引发异常…第二种方法,via WebDriver起作用…第二种解决方案中有一个输入错误…您离开了驱动程序。findElement…谢谢!!哦,那么你可以选择WebDriver?酷。我很高兴能帮上忙!感谢您提供的解决方案…我猜您使用的是不同版本的Selenium…我使用的是2.2…未找到waitFor方法…请您提供最新的方法…我的上帝!waitFor()方法是我自己提供的,它不是selenium API。我在代码中提供了该方法,您如何可以忽略它。谢谢!!:)你的解决方案看起来很好,我一定会研究一下,然后再回来……)
I.innerHTML = '<a href="' + D + "...a loong piece of URL..." + H + '">Sign In</a>';
long targetTime = System.currentTimeMillis() + 10000; // 10 seconds from now
boolean found = false;
while (System.currentTimeMillis() < targetTime && !found) {
if (selenium.isElementPresent("link=Sign In")) {
selenium.click("link=Sign In");
found = true;
}
}
if (!found) {
throw new SeleniumException("Element not found!");
}
// open Firefox
WebDriver driver = new FirefoxDriver();
// set the Implicit wait to 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.washingtonpost.com/");
driver.findElement(By.linkText("Sign In")).click();
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginTest {
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final int PAUSE = 10;
@Test
public void testWebsiteTwo() throws Exception {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(PAUSE, TimeUnit.SECONDS);
driver.get("http://www.washingtonpost.com/");
WebElement signin = driver.findElement(By
.cssSelector("li#global-signin a"));
signin.click();
// this line is crucial so that Selenium can enter the username / password
WebDriver.TargetLocator locator = driver.switchTo();
locator.frame(0);
WebElement name = driver.findElement(By.cssSelector("input#username"));
name.sendKeys(USERNAME);
WebElement password = driver.findElement(By.cssSelector("input#password"));
password.sendKeys(PASSWORD);
WebElement form = driver.findElement(By.cssSelector("form[name=form]"));
form.submit();
Thread.sleep(5000);
driver.close();
}
}