简体   繁体   中英

How to click on a href link by using python scripts and selenium?

I am trying to get Selenium to click on a specific href such as

<a href="publications.html">Publications</a>

I tried doing

driver.find_element_by_link_text('Publications.html').click()

but it gives me the error:

AttributeError: 'NoneType' object has no attribute 'click'.

Any suggestions?

.find_element_by_link_text() finds an A tag by the text inside the A tag, eg

<a href="publications.html">Publications</a>

You can find this tag using

driver.find_element_by_link_text("Publications")

BUT... if you want to find an A tag by the href, you need a different approach.

driver.find_element_by_css_selector("a[href='publications.html']")

This is a CSS selector. You can find more info on them in the links below.

CSS Selector Reference

CSS Selector Tips

Change

find_element_by_link_text('Publications.html')

to

find_element_by_link_text('Publications')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM