获取selenium的链接(xpath)并单击(python 2.7)
对于我的练习,我必须使用selenium和Chrome webdriver with python 2.7单击以下链接: 下面是html文件的结构:获取selenium的链接(xpath)并单击(python 2.7),python,selenium,webdriver,Python,Selenium,Webdriver,对于我的练习,我必须使用selenium和Chrome webdriver with python 2.7单击以下链接: 下面是html文件的结构: <div class="leftside" > <span class="spacer spacer-20"></span> <a href="https://test.com" title="Retour à l'accueil"><img class="logo" src
<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>
但我有一条错误信息:
SyntaxError:未能对“文档”执行“评估”:字符串
“//[@id=“leftside”]/a[3]”不是有效的XPath表达式
谁能帮我
关于问题是您还需要一个标记名。所以你应该使用
driver.find_element_by_xpath('//*[@id="leftside"]/a[3]').click()
当你不在乎它是哪个标签的时候。或者你应该使用实际的标签,如果你在乎的话
driver.find_element_by_xpath('//div[@id="leftside"]/a[3]').click()
我建议不要使用带有
div
标记的第二个。由于xpath比较慢,使用*
可能会稍微慢一些我也遇到了类似的问题,但我没有发布它,所以感谢您的帖子
xpath是
“/[@class=“leftside”]/a[3]”
在html中没有名为leftside的id。我想你很接近了。但由于它是一个
标记,class属性设置为leftside,因此您必须具体说明。但同样地,
标记不会是驱动程序的直接子项。通过xpath(“//div[@class='leftside']
节点查找元素,而是一个decentent,因此,您必须按如下方式导出/
:
driver.find_element_by_xpath("//div[@class='leftside']//a[3]").click()
driver.find_element_by_xpath("//div[@class='leftside']//a[3]").click()