javascript - Python + Selenium WebDriver,点击提交按钮

标签 javascript jquery python selenium xpath

我正在开发一个网络抓取工具,我需要点击提交按钮来触发 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>
     &nbsp;
</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()

最佳答案

"btn btn-primary"button的类名,不是div。尝试

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

//button[.="Search"]

关于javascript - Python + Selenium WebDriver,点击提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48050870/

相关文章:

php - 从目录收集图像的 javascript 图像 slider

python - python网页报错

javascript - 将图像固定在具有特定坐标、比例的图像上?

javascript - 从 URL 输入渲染/预览图像

javascript - For 循环不会结束使用 Node.js

python - Python 中的二进制相移键控

python - django excel xlwt

javascript - 获取工作表的范围,直到包含数据的最后一个单元格

javascript - 键数组,在 json 对象中查找值

javascript - 在 Chrome 中,聚焦 HTML 文本输入字段可选择内容。如何取消选择?