Selenium不会点击按钮

时间:2014-03-05 16:12:21

标签: python selenium onclick click selenium-webdriver

我正试图从这个网页抓取数据...... “http://agmarknet.nic.in/mark2_new.asp

我需要在商品搜索中输入“banana”并点击“Go”按钮。

在Stack Overflow的帮助下,我可以调出Firefox,键入“Banana”......但是“Go”按钮(基于检查的Go3)将不会消失!!

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

OTHER搜索按钮(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)

1 个答案:

答案 0 :(得分: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)

希望有所帮助。