简体   繁体   中英

Python + Selenium WebDriver, click on submit button

I am working on a web scraper where I need to click the submit button to trigger a JavaScript script to load a page. I am not able to identify the right tag and perform the click() function. I am thinking this has to do something with the aria-hidden="true" tag. Can you please let me know how we can achieve this?

Here is the page_source section for the button:

<div>   
     <button type="button" class="btn btn-primary" onclick="javascript:document.forms[0].submit();">
        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>Search
     </button>
     &nbsp;
</div>

Here is the code that I have:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.PhantomJS(executable_path='C:/Users/50178/Documents/learn/pracpy/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe')
#driver = webdriver.Chrome(executable_path='C:/Users/50178/Documents/learn/pracpy/ChromeDriver/chromedriver.exe')
driver.get("http://www16.co.hennepin.mn.us/cfrs/search.do")
driver.implicitly_wait(5)
driver.find_element_by_css_selector("input[type='radio']").click()
print(driver.page_source)
print (driver.find_element_by_xpath('.//div[@class="btn btn-primary"]'))
driver.close()

"btn btn-primary" is a class name of button , but not div . Try

//button[@class="btn btn-primary"]

or

//button[.="Search"]

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