【问题标题】:How to click a element based on href text?如何根据 href 文本单击元素?
【发布时间】:2017-05-30 16:33:17
【问题描述】:

使用 C#,我试图单击具有此文本的元素:

<a target="_blank" href="http://client.mysite.com/nimda/settings.php?campaign=level+1;uid=vmrgssaxs8wkb45oy77pl5xuu&amp;id=172806#rotate"><img alt="Click to edit the rotator settings" title="Click to edit the rotator settings" src="../gfx/icons/small/rotation.png"></a>

所以我尝试使用这个表达式:

driver.FindElement(By.XPath("(//a[contains(@href, 'level 1')]")).Click();

但是我得到了无效的表达,为什么?

请注意,我想使用 xpath,因为页面中有很多 a 元素,并且除了 href 值之外,所有元素都具有相同的属性

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    括号有问题

    driver.FindElement(By.XPath("//a[contains(@href, 'level 1')]")).Click();
    

    顺便说一句,你可以通过href属性找到元素,使用CssSelector

    driver.FindElement(By.CssSelector("[href*='level 1']")).Click();
    

    【讨论】:

    • 我必须写像level+1这样的值
    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2020-03-14
    • 2016-07-17
    • 1970-01-01
    • 2012-06-27
    相关资源
    最近更新 更多