【问题标题】:Unable to find gif image by Xpath/classname/cssSelector using selenium webdriver java使用 selenium webdriver java 无法通过 Xpath/classname/cssSelector 找到 gif 图像
【发布时间】:2019-05-24 15:10:53
【问题描述】:

我正在尝试使用 Selenium webdriver 定位图像,但无法通过 Xpath/cssSelector 定位它

我已经尝试过 cssSelector 和 xpath,但都不起作用。

<img alt="" class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://tpc.googlesyndication.com/simgad/303052068860032968">

通过 cssSelector -->

WebElement elementOut = driver.findElement(By.cssSelector(".i-amphtml-fill-content.i-amphtml-replaced-content"));

通过 Xpath -->

WebElement elementOut = driver.findElement(By.xpath("//*[@id='aw0']/amp-img/img"));

我需要定位图像。

页面源截图:

【问题讨论】:

  • 你能把你的图片所在页面的HTML代码贴出来吗?否则,将无法在这方面为您提供帮助。
  • HTML 代码:- tpc.googlesyndication.com/simgad/303052068860032968">
  • 这只是&lt;img&gt; 标签。这不足以定位图像。您需要提供的是整个页面的 HTML 代码,或者至少是包含图像的所有 HTML 标记。另外,请将该代码添加到您的原始帖子中(您应该能够对其进行编辑,而不是在评论中)。这将使其更具可读性,人们将能够更轻松地为您提供帮助。
  • 添加了详细页面来源的链接
  • 您需要先选择iframe

标签: java selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

您的图片位于iframe

因此,在尝试定位 iframe 内的元素之前,您需要执行 driver.switchTo() 函数。

完成后,您应该可以使用 XPath expression 之类的:

driver.findElement(By.xpath("//img[contains(@class,'replaced-content')]"));

【讨论】:

  • 按照建议,我已切换到 iframe 并尝试定位该元素,但我仍然无法定位该元素。我的代码如下: driver.switchTo().frame("google_ads_iframe_/8900/24.com/Web/News24/Homepage_20");driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);WebElement elementOut = driver.findElement(By.xpath("//div[contains(@id,'DfaVisibilityIdentifier_3594674463')]"));
  • 他们更新了代码,所以我的 xpath 与以前版本的代码不同。
  • 图片代码:s0.2mdn.net/6075616/…" alt="Advertisement" border="0" width="278" height="100" style="" xpath="1">
【解决方案2】:

到图像上的click(),因为所需的元素在&lt;iframe&gt; 内,所以您必须:

  • 为所需的 frameToBeAvailableAndSwitchToIt 诱导 WebDriverWait
  • 为所需的 elementToBeClickable 诱导 WebDriverWait
  • 您可以使用以下任一Locator Strategies

    • cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id$='com/Web/News24/Homepage_20'][name^='google_ads_iframe_'][title='3rd party ad content']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("img.i-amphtml-fill-content.i-amphtml-replaced-content[src^='https://tpc.googlesyndication.com/simgad']"))).click()
      
    • xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@id, 'com/Web/News24/Homepage_20') and starts-with(@name, 'google_ads_iframe_')][@title='3rd party ad content']")));
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//img[@class='i-amphtml-fill-content i-amphtml-replaced-content' and starts-with(@src, 'https://tpc.googlesyndication.com/simgad')]"))).click();
      

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多