【问题标题】:Selenium will NOT click buttonSelenium 不会点击按钮
【发布时间】:2014-03-05 16:12:21
【问题描述】:

我正在尝试从该网页中抓取数据... "http://agmarknet.nic.in/mark2_new.asp"

我需要在商品搜索中输入“香蕉”,然后点击“开始”按钮。

在 Stack Overflow 的帮助下,我可以启动 Firefox,输入“Banana”...但是“Go”按钮(基于检查的 Go3)不会触发!!

我尝试过 element.click(),我尝试过 ActionChains,我尝试过将光标移动到元素上,我已经验证它已启用。它只是不会进入下一个搜索页面。

其他搜索按钮(B1 ......这是一个通用搜索)有点工作......除了当 selenium 点击它时,它会打开一个与我手动点击它时不同的页面......所以这也很奇怪.

我没有收到任何错误...它只是没有转到下一页。

提前感谢您提供的任何帮助。快把我逼疯了!

def SLEEP(num):
    for i in range(0,num,1):
        print ".",
        time.sleep(1)

def click_button(driver, button_name):
    assert driver.find_element_by_name(button_name)
    button = driver.find_element_by_name(button_name)

    if button.is_enabled():
        print "it is enabled"
    else:
        print "IT IS NOT ENABLED"

    # Try with element
    button.click()
    #Try with action chain
    action = ActionChains(driver)
    action.move_to_element(driver.find_element_by_name(button_name))
    action.click(driver.find_element_by_name(button_name))
    action.perform()

# WORKS
driver = webdriver.Firefox()
driver.get("http://agmarknet.nic.in/mark2_new.asp")
SLEEP(5)
assert "AG" in driver.title
print driver.title

# WORKS
textinput = driver.find_element_by_name('cmm')
textinput.send_keys("banana")
SLEEP(5)

# SORT OF WORKS (brings up unexpected page)
button_name = "B1"
click_button(driver, button_name)

# DOES NOT WORK
button_name = "Go3"
click_button(driver, button_name)

【问题讨论】:

  • 我同意 alecxe 的观点,您的问题无法重现。

标签: python selenium onclick click selenium-webdriver


【解决方案1】:

button.click() 为我工作。请注意,您不需要在操作之间放置time.sleep

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("http://agmarknet.nic.in/mark2_new.asp")

textinput = driver.find_element_by_name('cmm')
textinput.send_keys("banana")

button_name = "Go3"
button = driver.find_element_by_name(button_name)
button.click()

另外,你可以按空格键代替click()

from selenium.webdriver.common.keys import Keys

...

button.send_keys(Keys.SPACE)

希望对您有所帮助。

【讨论】:

  • 谢谢!很高兴知道。也许是因为我的 Eclipse 快用完了。让我从命令行尝试一下...
  • 我换成了 phantomjs 并且它可以工作。不知道我的 Firefox 出了什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2014-02-16
  • 2016-05-08
  • 2021-09-14
  • 2018-05-11
  • 2021-02-26
相关资源
最近更新 更多