无法使用 Xpath 和 Selenium 在 iframe 中找到元素

拉夫尼特·考尔

我正在尝试检查 iframe 中的一个元素我能够成功进入 iframe 但是当尝试检查元素时 - 异常来了元素无法交互 下面是 html 部分和我试图点击的搜索按钮 在此处输入图片说明

我尝试了 xpath 和检查,如下所示

//driver.findElement(By.className("gsc-search-button-v2")).click();
        //driver.findElement(By.cssSelector("button.gsc-search-button")).click();
        //driver.findElement(By.xpath("//button[text()='gsc-search-button']")).click();

        //driver.findElement(By.cssSelector("button[class='gsc-search-button gsc-search-button-v2']")).click();
//driver.findElement(By.cssSelector(".gsc-search-button.gsc-search-button-v2")).click();

//driver.findElement(By.xpath("//button[contains(@class, 'gsc-search-button gsc-search-button-v2")).click();
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);

Also by giving waits also like
WebDriverWait wait = new WebDriverWait(driver, 30);
    //wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);

html代码 在此处输入图片说明

德班扬B

click()在要搜索的元素上,因为所需的元素在 a 内,<iframe>所以您必须:

  • 诱导WebDriverWait用于期望frameToBeAvailableAndSwitchToIt
  • 为所需的elementToBeClickable引入WebDriverWait
  • 您可以使用以下任一定位器策略
    • 使用cssSelector

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
      
    • 使用xpath

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='gsc-search-button']/button[@class='gsc-search-button gsc-search-button-v2']"))).click();
      

参考

您可以在以下位置找到一些相关讨论:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Selenium 无法在 iframe 中找到元素

无法在 IFrame (Selenium) 中找到元素

使用Selenium WebDriver Python无法在iframe中找到元素

无法在 python selenium 中使用 xpath 和 id 定位 iframe

无法在 Selenium 中找到 xPath

Python Selenium 无法通过类名和 xpath 找到元素

无法使用 xpath 找到元素 selenium python

无法在Selenium Webdriver(java)中找到iframe

如何使用Xpath Selenium和Python找到span元素

无法通过iFrame中的xpath单击元素-Selenium

使用Selenium和Python无法通过xpath定位元素

如何在Selenium中找到div格式的元素。出现错误无法使用xpath表达式定位元素

使用Selenium和Python无法在#shadow-root(打开)中找到Sign In元素

使用Xpath(Selenium C#)在<iframe>内找到一个HTML元素

Selenium Python如何使用XPath从表中的HTML表中找到正确的复选框元素

如何使用Selenium Xpath在一页中找到具有相同路径的乘法元素

硒无法在iframe中找到字典元素

Selenium 无法通过 xpath 或类找到元素

无法在 Selenium 中找到按钮和 href

无法在Selenium中找到元素(htmlUnitDriver)

Selenium:Python无法从elif中找到元素

无法在Selenium Python中找到元素

无法在Selenium Python中找到元素

Selenium WebDriver无法在链接中找到元素

使用Selenium和Java无法找到元素:{“ method”:“ xpath”,“ selector”:“ // li [@ id =” tablist1-tab3“]”}错误

如何使用Selenium和Python在https://www.shopdisney.com/中找到“创建帐户”元素

如何找到通过文字忽略了使用Selenium和XPath的情况下元素

我如何从指定的XPATH中找到所有元素,但是使用Selenium接受略有变化的ID?

无法使用selenium和python通过sendkeys将文件上传到iframe中的按钮元素中