Reputation: 343
For my exercice I must with selenium and Chrome webdriver with python 2.7 click on the link :
Below structure of the html file :
<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>
I use : driver.find_element_by_xpath() as explained here : enter link description here
driver.find_element_by_xpath('//[@id="leftside"]/a[3]').click()
But I have this error message :
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id="leftside"]/a[3]' is not a valid XPath expression.
Who can help me please ?
Regards
Upvotes: 1
Views: 102
Reputation: 193108
I think you were pretty close. But as it is a <div>
tag with class attribute set as leftside you have to be specific. But again the <a[3]>
tag won't be the immediate child of driver.find_element_by_xpath("//div[@class='leftside']
node but a decendent, so instead of /
you have to induce //
as follows :
driver.find_element_by_xpath("//div[@class='leftside']//a[3]").click()
Upvotes: 1
Reputation: 131
i have got a similar problem but i didn't post it so thanks for the post.
the xpath is "//[@class="leftside"]/a[3]"
there is no id with the name leftside in your html.
Upvotes: 0
Reputation: 146510
The issue is that you need to have a tag name also. So you should either use
driver.find_element_by_xpath('//*[@id="leftside"]/a[3]').click()
When you don't care about which tag it is. Or you should use the actual tag if you care
driver.find_element_by_xpath('//div[@id="leftside"]/a[3]').click()
I would suggest against using the second one, with the div
tag. As xpath are slower and using a *
may be slightly slower
Upvotes: 0