selenium.WebDriverException:无法将返回值转换为WebElement

时间:2019-10-02 18:22:47

标签: selenium selenium-webdriver salesforce-lightning

尝试查找元素时看到以下错误。在这里,我想在找到Web元素后填充文本。

我能够通过chrome控制台找到xpath,这是我如何解决此问题的一些方法。关于此问题有一些帖子,但是大多数帖子与鸦片有关,与我无关。

    util.driver.switchTo().defaultContent();
    util.driver.switchTo().frame(0);
    util.driver.findElement(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input"));

错误消息:

org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {error=no such element, message=no such element: Unable to locate element: {"method":"xpath","selector":"//label[text()='Reason for Escalation']/following-sibling::div/input"}

Driver info: driver.version: RemoteWebDriver
  at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:324)
  at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
  at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
  at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)


Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
  at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
  at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
  at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
  at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)

enter image description here

页面上有3个iframe,正在访问的元素位于第一页

1 个答案:

答案 0 :(得分:1)

在切换到iframe之前,我会尝试在其上调用WebDriverWait。

// wait for iframe to exist, then switch to it

WebDriverWait wait = new WebDriverWait(util.driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.Xpath("//iframe[contains(@name, 'vfFrameId')]")));

// wait for element to exist

element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input")));