【问题标题】:Image, src, href in same span. How to locate? Webdriver, css locator图像、src、href 在同一跨度中。如何定位? Webdriver,css定位器
【发布时间】:2017-02-07 10:04:44
【问题描述】:

我尝试了多种方式在 Google 图片上click,但到目前为止没有运气?请问您能帮忙吗?

<div class="AppLogos">
    <span class="iTunesLogo">
    <span class="GoogleLogo">
        <a href="https://play.google.com/store/apps/details?id=com.sportsdirect.sdapp&hl=en&utm_source=global_co&utm_medium=prtnr&utm_content=Mar2515&utm_campaign=PartBadge&pcampaignid=MKT-Other-global-all-co-prtnr-py-PartBadge-Mar2515-1" target="_top">
<img class="img-responsive" alt="Get it on Google Play" src="https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png">
        </a>
    </span>
</div>

【问题讨论】:

  • 您提到我曾尝试以多种方式点击 Google 图片,但到目前为止都没有成功,您能否在此分享一下您目前尝试过哪些?

标签: xpath selenium-webdriver css-selectors webdriver


【解决方案1】:

来自评论:-

www.sportsdirect.com - 我想在启动时定位谷歌应用程序徽标(第二个弹出窗口)

(假设您使用的是 JAVA)实际上这个弹出窗口在 &lt;iframe&gt; 内,这就是您无法找到该元素的原因。您应该尝试使用WebDriverWait 等到这个&lt;iframe&gt; 出现与欲望弹出窗口,然后切换到那个&lt;iframe&gt; 之前找到如下的欲望元素:-

driver.get("http://www.sportsdirect.com/");
driver.manage().window().maximize()

WebDriverWait wait = new WebDriverWait(driver, 60)

//now wait for pop visible with iframe and then switch to it
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='appadvert']")));

//now wait for desire element visible and enable to click
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.GoogleLogo > a"))).click();

【讨论】:

    【解决方案2】:

    我不知道你使用的是什么语言,但对于 C#:

    IWebElement googleLogo = driver.FindElement(By.ClassName("img-responsive"));
    googleLogo.Click();
    

    应该可以工作,这取决于 html 的其余部分,以及该类是否出现在其他地方。 XPath 应该可以正常工作。

    【讨论】:

    • 行不通。同一类出现在“iTunesLogo”跨度中。 Xpath 也不能正常工作。还有其他想法吗?
    • @Supermint,可能是 XPATH 以及父标签可能会起作用。你有机会尝试吗?
    • @Siva 感谢您的回复。 Xpath 是://*[@id='dnn_ctr13489121_HtmlModule_lblContent']/div/div[3]/span[2]/a/img 与父级的关系如何?
    • 尝试使用 XPath 定位它时遇到什么错误?
    • @Supermint,我的意思是//span[@class='GoogleLogo']/a/img[@class='img-responsive']。但是如果你使用的 XPATH 不工作,我建议工作的机会也很低。请试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2018-01-05
    • 2012-07-20
    相关资源
    最近更新 更多