简体   繁体   中英

Selenium and Python can't click href=“javascript:void(0);” with .click()

I have been stuck on trying to click the href="javascript:void(0);" on a page.

My problem is that I am not able to select this using selenium, and my end goal is to click on one. The page is fully loaded, and this is what all the links on the page have as the href.

my code is this:

a = soup.find_all('a')
for names in a:
    try:
        print (names['href'])
        if names['href'] == "javascript:void(0);":
            print "IM IN HUR"
            names.click()
            break
    except:
        continue

But the "name.click()" statement never works.I have not found any way to click on javascript:void(0). Any help would be much appreciated.

You have to use Selenium WebDriver to interact with web browser. Your variable names is an object of Beautiful Soup which is a Python package for parsing HTML/XML, it cannot interact with web browser. Try code below:

aElements = browser.find_elements_by_tag_name("a")
for name in aElements:
    if(name.get_attribute("href") is not None and "javascript:void" in name.get_attribute("href")):
        print("IM IN HUR")
        name.click()
        break

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