1

For my exercice I must with selenium and Chrome webdriver with python 2.7 click on the link :

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

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

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.