【问题标题】:Get link with selenium (xpath) and click (python 2.7)获取与 selenium (xpath) 的链接并单击 (python 2.7)
【发布时间】:2018-03-29 13:40:24
【问题描述】:

对于我的练习,我必须使用 selenium 和 Chrome webdriver 以及 python 2.7 点击链接:

https://test.com/console/remote.pl

html文件结构如下:

<div class="leftside"  >
    <span class="spacer spacer-20"></span>
    <a href="https://test.com" title="Retour à l'accueil"><img class="logo" src="https://img.test.com/frontoffice.png" /></a>
    <span class="spacer spacer-20"></span>
    <a href="https://test.com/console/index.pl" class="menu selected"><img src="https://img.test.com/icons/fichiers.png" alt="" /> Mes fichiers</a>
    <a href="https://test.com/console/ftpmode.pl" class="menu"><img src="https://img.test.com/icons/publication.png" alt="" /> Gestion FTP</a>
    <a href="https://test.com/console/remote.pl" class="menu"><img src="https://img.test.com/icons/telechargement-de-liens.png" alt="" /> Remote Upload</a>
    <a href="https://test.com/console/details.pl" class="menu"><img src="https://img.test.com/icons/profil.png" alt="" /> Mon profil</a>
    <a href="https://test.com/console/params.pl" class="menu"><img src="https://img.test.com/icons/parametres.png" alt="" /> Paramètres</a>
    <a href="https://test.com/console/abo.pl" class="menu"><img src="https://img.test.com/icons/abonnement.png" alt="" /> Services Payants</a>

<a href="https://test.com/console/aff.pl" class="menu"><img src="https://img.test.com/icons/af.png" alt="" /> Af</a>
<a href="https://test.com/console/com.pl" class="menu"><img src="https://img.test.com/icons/v.png" alt="" /> V</a>
    <a href="https://test.com/console/logs.pl" class="menu"><img src="https://img.test.com/icons/logs.png" alt="" /> Jour</a>
    <a href="https://test.com/logout.pl" class="menu"><img src="https://img.test.com/icons/deconnexion.png" alt="" /> Déconnexion</a>
    <span class="spacer spacer-20"></span>
    <a href="#" id="msmall"><img src="https://img.test.com/btns/reverse.png"></a>
</div>

我使用:driver.find_element_by_xpath(),如下所述:enter link description here

driver.find_element_by_xpath('//[@id="leftside"]/a[3]').click()

但是我有这个错误信息:

语法错误:无法对“文档”执行“评估”:字符串 '//[@id="leftside"]/a[3]' 不是有效的 XPath 表达式。

谁能帮帮我?

问候

【问题讨论】:

    标签: python selenium webdriver


    【解决方案1】:

    我认为你很接近。但由于它是一个&lt;div&gt; 标签,其 class 属性设置为 leftside,因此您必须具体说明。但同样&lt;a[3]&gt; 标记不会是driver.find_element_by_xpath("//div[@class='leftside'] 节点的直接子节点,而是一个后代,因此您必须诱导// 而不是/,如下所示:

    driver.find_element_by_xpath("//div[@class='leftside']//a[3]").click()
    

    【讨论】:

      【解决方案2】:

      问题是您还需要有一个标签名称。所以你应该使用

      driver.find_element_by_xpath('//*[@id="leftside"]/a[3]').click()
      

      当您不关心它是哪个标签时。或者你应该使用实际的标签,如果你在乎的话

      driver.find_element_by_xpath('//div[@id="leftside"]/a[3]').click()
      

      我建议不要使用带有div 标签的第二个。由于 xpath 较慢,使用 * 可能会稍慢

      【讨论】:

        【解决方案3】:

        我遇到了类似的问题,但我没有发布,所以谢谢你的帖子。

        xpath 是 "//[@class="leftside"]/a[3]" 在您的 html 中没有名称为 leftside 的 id。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-22
          • 2021-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多