我正在使用网络抓取工具,我需要点击提交按钮以触发JavaScript脚本来加载网页。我无法识别正确的标签并执行click()
功能。我认为这必须与aria-hidden="true"
标签做一些事情。能告诉我们我们如何实现这一目标吗?
以下是按钮的page_source部分:
<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>
</div>
以下是我的代码:
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()
答案 0 :(得分:1)
"btn btn-primary"
是button
的班级名称,但不是div
。尝试
//button[@class="btn btn-primary"]
或
//button[.="Search"]