简体   繁体   English

如何使用 Selenium 和 Python 2.7 单击表单中的按钮?

[英]How do I click a button in a form using Selenium and Python 2.7?

I am trying to create a Python program that periodically checks a website for a specific update.我正在尝试创建一个 Python 程序,它会定期检查网站的特定更新。 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');">

I have tried numerous ways to access the button and always get "selenium.common.exceptions.NoSuchElementException:" error.我尝试了多种访问按钮的方法,但总是收到“selenium.common.exceptions.NoSuchElementException:”错误。 The obvious approach to access the button is XPath and using the Chrome X-Path Helper tool, I get the following:访问按钮的明显方法是 XPath 并使用 Chrome X-Path Helper 工具,我得到以下信息:

/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']

If I include the above as follows:如果我包括上述内容如下:

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()

I still get the NoSuchElementException error.我仍然收到 NoSuchElementException 错误。

I am new to selenium and so there could be something obvious that I am missing;我是 selenium 的新手,所以我可能缺少一些明显的东西; however, after much Googling, I have not found an obvious solution.然而,经过多次谷歌搜索,我还没有找到明显的解决方案。

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.另一方面,我也尝试过 find_element_by_name('manageAptm') 和 find_element_by_class_name('bluebutton') 并且都给出了相同的错误。

Can someone advise on how I can effectively click this button with Selenium?有人可以建议我如何使用 Selenium 有效地单击此按钮吗?

Thank you!谢谢!

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 towait for it :为了遵循您的尝试和@har07 的评论, find_element_by_name('manageAptm')应该可以工作,但该元素可能不会立即可用,您可能需要等待它

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()

Also, check if the element is inside an iframe or not.另外,检查元素是否在iframe内。 If yes, you would need to switch into the context of it and only then issue the "find" command:如果是,您需要切换到它的上下文,然后才发出“find”命令:

driver.switch_to.frame("frame_name_or_id")
driver.find_element_by_name('manageAptm').click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM