我正面临着在chrome中定位元素的问题 . 元素的XPath看起来像这样:
//*[@id="signin"] (right click on the element and copy xpath)
相同的CSS:
$$("div[id='signin']")
现在,当我手动将鼠标悬停在Chrome上时,我可以在Chrome中找到该元素 . 但是,当我尝试实现与代码相同时,它会抛出错误 .
driver.findElement(By.cssSelector("$$("div[id='signin']")).click(); - CSS
driver.findElement(By.xpath("//*[@id="signin"]")).click(); - XPATH
它会抛出 remove argument to match 'xpath(String)'
帮助!!
Code :
public static WebDriver driver;
@BeforeClass
public static void start()
{
File file = new File("D:/new/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.get("http://abcd.com");
}
@AfterClass
public static void close()
{
driver.close();
}
@Test
public static void test()
{
driver.findElement(By.name("UserId")).sendKeys("100");
driver.findElement(By.name("Password")).sendKeys("100");
driver.findElement(By.xpath("//*[@id='signin']")).click();
}
}
@NOte : the URL here is dummy. in the real time am using a proper URL. It throws the below error
FAILED:test org.openqa.selenium.NoSuchElementException:无法找到该元素(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:89毫秒有关此错误的文档,请访问:http:// seleniumhq.org/exceptions/no_such_element.html构建信息:版本:'2.32.0',修订版:'6c40c18',时间:'2013-04-09 17:23:22'系统信息:os.name:'Windows 7 ',os.arch:'x86',os.version:'6.1',java.version:'1.7.0'
3 回答
尝试用单引号括起
signin
而不是双引号,如下所示 .从您提到的CSS看来,它似乎是您想要找到的div元素 . 以下应该无缝地为此工作 .
. driver.findElement(By.xpath( “// DIV [@ ID = '登入']”))点击();
driver.findElement(By.cssSelector( “#DIV登入”))点击() . - CSS
. driver.findElement(By.xpath( “// * [@ id中= '登入']”))点击(); - Xpath
. driver.findElement(By.id( “登入”))点击(); - ID