Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java/selenium获取chrome和edge中的焦点元素_Java_Google Chrome_Selenium_Selenium Webdriver_Microsoft Edge - Fatal编程技术网

使用java/selenium获取chrome和edge中的焦点元素

使用java/selenium获取chrome和edge中的焦点元素,java,google-chrome,selenium,selenium-webdriver,microsoft-edge,Java,Google Chrome,Selenium,Selenium Webdriver,Microsoft Edge,在chrome和FF中运行测试时,我遇到无法聚焦元素错误。它工作正常。我试过发布决议,但没有用。我不知道该怎么办。希望有人能帮忙。提前谢谢 driver.findElement(By.linkText("Add")).click(); List <WebElement> groups = new Select(driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/

在chrome和FF中运行测试时,我遇到无法聚焦元素错误。它工作正常。我试过发布决议,但没有用。我不知道该怎么办。希望有人能帮忙。提前谢谢

driver.findElement(By.linkText("Add")).click();
List <WebElement> groups = new Select(driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[1]/select"))).getOptions();
groups.get(3).click();
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement groupRole = driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[2]/label[2]"));
js.executeScript("arguments[0].click();", groupRole);
driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[2]/div/div/div[2]/label[2]")).sendKeys(" ");
// Check to see if the user should be made active and set active checkbox to on if value in file is "active"
if (activeFlag.equals("active"))
{
    driver.findElement(By.xpath("/html/body/div[1]/section/div/article/form/fieldset/div[3]/div[1]/div[1]/div/div/input")).sendKeys(" ");
} 
// If the user role is to be admin then set the Site role to Administrator
if (userLevel.equals("admin"))
{
    List <WebElement> roles = new Select(driver.findElement(By.name("community_role"))).getOptions();
    roles.get(1).click();
}
这里是另一个堆栈跟踪。实际上,我在尝试单击submit按钮而不是active标志时遇到了错误。我已经完成了我还将提供的操作步骤

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
  (Session info: chrome=47.0.2526.106)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 52 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12'
System info: host: 'Janet-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.7.0_80-ea'
Session ID: f332b496aa54d581c764f7328e770e65
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=WIN8_1, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Janet\AppData\Local\Temp\scoped_dir2072_18936}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=47.0.2526.106, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:121)
    at Viddler_create_user.createUser.test(createUser.java:244)
    at Viddler_create_user.createUser.setUpBeforeClass(createUser.java:126)
    at Viddler_create_user.createUser.main(createUser.java:64)
代码:


根据堆栈跟踪,sendkeys方法是问题所在

at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:121)
请尝试
Actions
class以首先关注元素,然后发送所需的键

Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys("SOME DATA");
actions.build().perform();

行动决议毕竟起了作用。显然,我有一个额外的driver.findElementBy行,它应该被注释掉,因为它与我移动到另一个位置的某个内容重复


谢谢你的帮助

为便于将来参考,如果其他人遇到此问题,请确保您只找到一个元素!Chrome工具有时在这个问题上是有欺骗性的。 我在firePath(firefox插件)中重新检查了我的选择器,结果发现我有两个匹配的节点,尽管chrome工具向我显示了一个元素


我在阅读“Action class”代码时发现了非常重要的代码

“动作类”之所以有效是因为

actions.click()  

试着放

element.click()
以前

element.sendKeys()
在现有代码中

click()方法使元素成为焦点~~
对于每一位高年级学生来说,选择的答案只对我起了部分作用。加入

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.clear();

在使用建议的答案之前,它完全有效

这是正确答案的编辑版本,因为我无法复制粘贴该代码。使用下面的代码,您可以复制粘贴它(如果元素被发现为id):


可能是您设置的Xpath不符合该元素级别。例如,如果一个文本框正在穿越
Div\Div\textarea
区域,那么很可能您只取了
Div\
部分。我也遇到了同样的问题,在将xpath写入
textarea
节点后,问题得到了解决。

Oh yeah忘了提到设置活动标志的元素发生了错误。在我添加上述代码将用户分配到组之前,这一切都很正常。请改进问题。你在哪里设置activeFlag。错误stacktrace can help.activeFlag包含在我正在读取的文件中。当我读取文件并拆分行时,所有变量都已设置。我将从Selenium.webdriver.common.action\u chains导入ActionChains中添加stack trace.Python、Selenium 3.8.1:
;actions=ActionChains(驱动程序)
对我很有用。非常感谢你帮我省了一天的时间。@a_程序员很高兴它能帮上忙。我以前用过一些难以触及的按钮。但现在我有了一个案例,但事实并非如此。这些行为完全不起作用,也不会产生任何错误。@FrankH。你的评论没有给出任何关于这个问题的背景。如果您认为您的问题与以前的问题不同,并且无法在以前的问题中找到答案,请在此基础上创建一个新问题
element.sendKeys()
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(element));
element.clear();
elem = driver.find_element_by_id("WHATEVER THE ELEMENT ID IS HERE")
actions = ActionChains(driver)
actions.move_to_element(elem)
actions.click()
actions.send_keys("PUT YOUR TEXT IN HERE")
actions.perform()