[英]Unable to find gif image by Xpath/classname/cssSelector using selenium webdriver java
我正在嘗試使用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"));
我需要找到圖像。
頁面源快照:
您的圖片位於iframe中
因此,在嘗試定位iframe內的元素之前,您需要執行driver.switchTo()函數。
完成后,您應該可以使用XPath表達式,例如:
driver.findElement(By.xpath("//img[contains(@class,'replaced-content')]"));
要在圖像上click()
,因為所需元素在<iframe>
因此您必須:
您可以使用以下定位策略之一 :
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.