繁体   English   中英

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

[英]Not able to find an element in iframe using Xpath and Selenium

我正在尝试检查 Iframe 中的一个元素 我能够成功进入 iframe 但是当尝试检查元素时 - 异常出现元素不可交互 下面是 ZFC35FDC70D5FC69D269883A822C7A53E 在此处输入图像描述

我试过 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代码在此处输入图像描述

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

  • 为所需的frameToBeAvailableAndSwitchToIt诱导WebDriverWait
  • 为所需的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();

参考

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM