Python:Selenium webDriver:按钮元素可访问,存在且未禁用但单击不起作用

时间:2016-08-29 14:50:46

标签: python selenium selenium-webdriver click

我想在以下网站上自动填写表格:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome('C:\\Users\\Math\\Documents\\chromedriver.exe')
driver.get('https://www.leboncoin.fr/')

我有一个功能,通过点击“Se connecter”按钮并填写我的密码和用户名自动连接。它工作正常。

然后我有另一个功能,点击按钮'Déposeruneannonce'并开始填写表格,其中包含所需的所有信息(desc,title,categ,price和images),一切正常,直到那时。这是代码:

def create_offer(driver,*otherargs):
    form_box = driver.find_element_by_xpath("//*[@id=\"header\"]/section/section/nav/ul/li[2]/a")
    form_box.click()

 (...) # code that fills in the form

    # problematic part
    submit_box = WebDriverWait(driver, 10).until(findsubmitbox)
    attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', submit_box)
    pprint(attrs)
    print (submit_box.get_attribute("disabled"))
    time.sleep(15)
    submit_box.click()
    # supposed to open a confirmation page after the click
    time.sleep(2) 
    check_box = driver.find_element_by_name('accept_rule')
    check_box.click()
    time.sleep(2)
    create_box = driver.find_element_by_name("create")
    create_box.click()
    time.sleep(2) 
    driver.get('https://www.leboncoin.fr/')

def findsubmitbox(driver):
    e = driver.find_element_by_xpath("//*[@id=\"newadSubmit\"]")
    if (e.get_attribute("disabled")=='true'):
        return False
    return e

问题在于,当我尝试点击“Valider”按钮来实际创建优惠并转到确认页面时,它不起作用。找到按钮但是点击不会触发任何内容。然后我的代码块显然是因为它无法找到下一个元素:

In [52]: create_offer(driver,frame,i)
{'class': 'button-blue',
 'id': 'newadSubmit',
 'type': 'submit',
 'value': 'Valider'}
None
Traceback (most recent call last):

  File "<ipython-input-52-20486118b46c>", line 1, in <module>
    create_offer(driver,frame,i)

  File "C:\Users\Math\Documents\Python Scripts\1st batch\pythonscripts1\leboncoin.py", line 144, in create_offer
    check_box = driver.find_element_by_name('accept_rule')

  File "C:\Users\Math\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)

  File "C:\Users\Math\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']

  File "C:\Users\Math\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)

  File "C:\Users\Math\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)

NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"accept_rule"}
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 6.1.7601 SP1 x86_64)

正如您所看到的,我已经尝试了How can I get Selenium Web Driver to wait for an element to be accessible, not just present?的答案,看看我试图点击的元素是否实际上是“禁用”但是当我尝试点击它时,禁用它不是其属性的一部分(我pprint所有的attribitues)。我还尝试让程序睡眠15秒,但没有任何东西能让它工作。

奇怪的是,在程序停止后,我在Ipython Shell中执行此操作并且工作正常:

submit_box = driver.find_element_by_xpath("//*[@id=\"newadSubmit\"]")
submit_box.click()

我缺少什么?

谢谢,

0 个答案:

没有答案