【问题标题】:How to click a link whose href contains some string in Selenium Python?如何单击其href包含Selenium Python中的某些字符串的链接?
【发布时间】:2016-09-20 21:17:49
【问题描述】:

我想为这个site点击网址中包含“930”的链接

<div id="liste_drives">
    <ul>
    <li class="">
        <span class="left">
        02 -
        <a href="http://www.tt.com/d/n/p/st-985">tt</a>
        <p class="a"> </p>
        </span>
        <a class="entre"  href="/d/m/St-985/">i enter here</a>  
    </li>
    <li class="">
        <span class="left">
        03 -
        <a href="http://www.tt.com/d/n/p/ee-930">tt</a>
        <p class="a"> </p>
        </span>
        <a class="entre"  href="/d/m/ee-930/">i enter here</a>
    </li>
</div>

我读过this answer

我试试这个:

id = "930"
driver.find_element_by_css_selector('"a[href*=%s]" %id').click()

但我无法点击网址

【问题讨论】:

    标签: python selenium selenium-webdriver css-selectors


    【解决方案1】:

    %id 部分应该在字符串之外,这样字符串格式才能正常工作:

    driver.find_element_by_css_selector("a[href*=%s]" % id)
    

    请注意,这假定 id 仅包含字母数字字符。如果不是,则需要将值括在引号中:

    driver.find_element_by_css_selector('a[href*="%s"]' % id)
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 2021-12-03
      • 2014-11-28
      • 2010-11-27
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多