python 使用Selenium单击输入类型提交按钮

icomxhvb  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(89)

试着点击这个

<input type="submit" value="Log In" class="btn btn-lg btn-primary btn-block">

我试过了

driver.find_element("class", "btn").click()

driver.find_element("class", "btn btn-lg btn-primary btn-block").click()

driver.find_element("value", "Log In").click()

但都给予错误

InvalidArgumentException: Message: invalid argument: invalid locator

我怎么能点击这个?
编辑:
现在我尝试

driver.find_element(By.CLASS_NAME, "btn btn-lg btn-primary btn-block").click()

但得到错误

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-lg btn-primary btn-block"}
f4t66c6m

f4t66c6m1#

试着用“By”

from selenium.webdriver.common.by import By

你可以用不同的方式选择你的元素:
按类别:

driver.find_element(By.CLASS_NAME, "btn btn-lg btn-primary btn-block").click()

通过XPATH:

driver.find_element(By.XPATH, '//input[@value="Log In"]').click()

通过CSS选择器:

driver.find_element(By.CSS_SELECTOR, 'input.btn.btn-lg.btn-primary.btn-block').click()

参考:https://selenium-python.readthedocs.io/locating-elements.html

相关问题