今天我更新我的铬驱动程序
If you are using Chrome version 77, please download ChromeDriver 77.0.3865.40
我尝试使用以下代码通过 id 查找元素:
public static void inputValueById(String input,String id)
{
WebDriver driver2 = WebDriverMgr.getDriver();
WebElement element = driver2.findElement(By.id("//input[@id='company']"));
element.click();
element.clear();
element.sendKeys(input);
}
我得到这个例外
ERROR: no such element: Unable to locate element: {"method":"css selector","selector":"#//input[@id='company']"}
(Session info: chrome=77.0.3865.90)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'C', ip: '10.9.26.18', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.90, chrome: {chromedriverVersion: 77.0.3865.40
有人可以告知为什么它通过 css 选择器而不是通过 ID 进行搜索吗? 方法":"css 选择器">
我认为当您尝试通过其ID查找元素时,您必须像这样编写代码:
driver.findElement(By.id("ID"))
如果你想用这个xpath
找到,你必须这样写:
driver.findElements(By.xpath("//input[@id='company']"));