0

I am trying to fill in a form automatically on the following website:

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

I have a function to connect automatically by clicking on the button 'Se connecter' and filling my pwd and username. It works fine.

Then I have another function that clicks on the button 'Déposer une annonce' and start filling in the form with all the info required (desc, title, categ, price and images), everything works well until then. Here is the code:

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

The problem is that when I try to click on the button 'Valider' to actually create the offer and go to the confirmation page it doesn't work. The button is found as present but click doesn't trigger anything. Then my code block obviously because it can't find the next element:

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)

As you can see I have tried the answer of How can I get Selenium Web Driver to wait for an element to be accessible, not just present? to see whether the element I was trying to click on was actually 'disabled' but disabled is not part of its attributes when I try to click on it (I pprint all the attribitues). I also tried to make the program sleep for 15 secs but nothing makes it work.

The weird part is that after the program stopped, I do this in the Ipython Shell and it works fine:

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

What am I missing on?

Thanks,

Community
  • 1
  • 1
Mth Clv
  • 625
  • 1
  • 7
  • 20

0 Answers0