如何使用Selenium和Python 2.7单击表单中的按钮? [英] How do I click a button in a form using Selenium and Python 2.7?
问题描述
我正在尝试创建一个Python程序,该程序会定期检查网站是否有特定更新.该站点是安全的,需要多次单击才能转到我要监视的页面.不幸的是,我一直试图找出如何单击特定按钮.这是按钮代码:
I am trying to create a Python program that periodically checks a website for a specific update. The site is secured and multiple clicks are required to get to the page that I want to monitor. Unfortunately, I am stuck trying to figure out how to click a specific button. Here is the button code:
<input type="button" class="bluebutton" name="manageAptm" value="Manage Interview Appointment" onclick="javascript:programAction('existingApplication', '0');">
我尝试了多种方法来访问该按钮,并始终收到"selenium.common.exceptions.NoSuchElementException:"错误.访问该按钮的明显方法是XPath,然后使用Chrome X-Path Helper工具获得以下信息:
I have tried numerous ways to access the button and always get "selenium.common.exceptions.NoSuchElementException:" error. The obvious approach to access the button is XPath and using the Chrome X-Path Helper tool, I get the following:
/html/body/form/table[@class='appgridbg mainContent']/tbody/tr/td[2]/div[@class='maincontainer']/div[@class='appcontent'][1]/table[@class='colorgrid']/tbody/tr[@class='gridItem']/td[6]/input[@class='bluebutton']
如果我包括以下内容:
browser.find_element_by_xpath("/html/body/form/table[@class='appgridbg mainContent']/tbody/tr/td[2]/div[@class='maincontainer']/div[@class='appcontent'][1]/table[@class='colorgrid']/tbody/tr[@class='gridItem']/td[6]/input[@class='bluebutton']").submit()
我仍然收到NoSuchElementException错误.
I still get the NoSuchElementException error.
我是硒的新手,所以可能有些明显的东西我很想念;但是,经过大量谷歌搜索之后,我还没有找到明显的解决方案.
I am new to selenium and so there could be something obvious that I am missing; however, after much Googling, I have not found an obvious solution.
另一方面,我也尝试过find_element_by_name('manageAptm')和find_element_by_class_name('bluebutton')并给出相同的错误.
On another note, I have also tried find_element_by_name('manageAptm') and find_element_by_class_name('bluebutton') and both give the same error.
有人可以建议我如何使用硒有效地单击此按钮吗?
Can someone advise on how I can effectively click this button with Selenium?
谢谢!
推荐答案
要遵循您的尝试和@ har07的注释,find_element_by_name('manageAptm')
应该可以工作,但是该元素可能无法立即使用,您可能需要等待它:
To follow your attempts and the @har07's comment, the find_element_by_name('manageAptm')
should work, but the element might not be immediately available and you may need to wait for it:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
manageAppointment = wait.until(EC.presence_of_element_located((By.NAME, "manageAptm")))
manageAppointment.click()
此外,检查元素是否在iframe
内部.如果是的话,您将需要切换到它的上下文,然后才发出"find"命令:
Also, check if the element is inside an iframe
or not. If yes, you would need to switch into the context of it and only then issue the "find" command:
driver.switch_to.frame("frame_name_or_id")
driver.find_element_by_name('manageAptm').click()
这篇关于如何使用Selenium和Python 2.7单击表单中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!