4

I am trying to fill in a form automatically on the following website: 'https://www.leboncoin.fr/'

I have recorded a script with Selenium IDE.

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

I have set up specific credential for this topic email: thecoingood@gmail.com pwd: thecoingood1

the code is

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Connectionwebdriver2(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Safari()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.base_urldr = "https://compteperso.leboncoin.fr/account/index.html"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_connectionwebdriver2(self):
        driver = self.driver
        driver.get(self.base_urldr)
        driver.find_element_by_name("st_username").clear()
        driver.find_element_by_name("st_username").send_keys("thecoingood@gmail.com ")
        driver.find_element_by_name("st_passwd").clear()
        driver.find_element_by_name("st_passwd").send_keys("thecoingood1")
        driver.find_element_by_id("connect_button").click()
        #driver.get("https://www2.leboncoin.fr/ai?ca=15_s")

        my_annonce = WebDriverWait(self.driver, 10)\
        .until(EC.element_to_be_clickable((By.LINK_TEXT, "Supprimer")))
        my_annonce.click()
        #time.sleep(10)
        #driver.find_element_by_link_text("Supprimer").click()
        #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='//https://compteperso.leboncoin.fr/account/index.html?ca=12_s' and contains(.,'posez une annonce')]"))).click()
        #Select(driver.find_element_by_id("category")).select_by_visible_text('Locations')
        #Select(driver.find_element_by_id('cat10')).select()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main(verbosity=2)

Once connected, I am redirected to https://compteperso.leboncoin.fr/account/index.html?ca=12_s (question: is the object used in selenium updated with this new address or still sticks with the initial one which may create the issue)

when I try to click on the

<a href="//www2.leboncoin.fr/ai?ca=15_s">Déposez une annonce</a>

with this code

driver.find_element_by_link_text(u"Déposez une annonce").click()

Nothing happens (no error).

I believe this is related to the fact the link is not yet visible. I have tried to add time.sleep() and also read

How can I get Selenium Web Driver to wait for an element to be accessible, not just present?

but coud not resolve this. I could add a direct link to the page, but I would like to understand.

Thanks in advance

Olivier Hoen
  • 127
  • 1
  • 1
  • 7

1 Answers1

1

As per your question to click on the tab / link with text as Déposez une annonce you can use the following line of code :

  • To click the TAB with text as Déposez une annonce use :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='deposer']/a[contains(@href,'//www2.leboncoin.fr/ai?ca=') and contains(.,'une annonce')]"))).click()
    
  • To click the BUTTON with text as Déposez une annonce use :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='create']/a[contains(@href,'//www2.leboncoin.fr/ai?ca=') and contains(.,'une annonce')]"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your answer. I have tried your code but still the same issue. I have updated the question, with credential created specifically for this question. The issue may be related to the accent or the the fact the page used to search the link is not exactly the one which is defined the set up. Thanks – Olivier Hoen Mar 03 '18 at 11:55
  • It works perfectly (both options). Thanks. For my education, you would say that my code was not working because of the accent? – Olivier Hoen Mar 03 '18 at 19:45