Selenium will NOT click button

被刻印的时光 ゝ 提交于 2019-12-11 19:43:25

问题


I'm trying to scrape data from this web page... "http://agmarknet.nic.in/mark2_new.asp"

I need to type in "banana" in the commodity search and click the "Go" button.

With the help of Stack Overflow, I'm able to bring up Firefox, type in "Banana"...but the "Go" button (Go3 based on the inspection) WILL NOT FIRE!!

I've tried element.click(), I've tried ActionChains, I've tried moving the cursor to the element, I've verified it's enabled. It just will not go to the next search page.

The OTHER search button (B1...which is a generic search) sort of works...except that when selenium clicks it, it brings up a different page than when I click it manually...so that's weird too.

I DON'T get any errors...it just does not go to the next page.

Thanks in advance for any help you can offer. It's driving me crazy!

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:


button.click() works for me. Note that you don't need to put time.sleep between actions:

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

Also, instead of click() you can push space button:

from selenium.webdriver.common.keys import Keys

...

button.send_keys(Keys.SPACE)

Hope that helps.



来源:https://stackoverflow.com/questions/22203080/selenium-will-not-click-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!