Home > Mobile >  webscrapping using python and selenium
webscrapping using python and selenium

Time:12-11

i create a small program in python using selenium module.

here is my code :

while True:
try:
    btnOptions = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div["
                                                                                           "1]/div/div["
                                                                                           "1]/div/div["
                                                                                           "5]/div/div/div["
                                                                                           "3]/div/div/div["
                                                                                           "1]/div["
                                                                                           "1]/div/div/div["
                                                                                           "4]/div[2]/div/div["
                                                                                           "2]/div[3]/div["
                                                                                           "3]/div/div/div/div"
                                                                                           "/div/div/div/div/div"
                                                                                           "/div/div["
                                                                                           "8]/div/div/div["
                                                                                           "2]/div/div["
                                                                                           "3]/div/div")))
    time.sleep(2)
    btnOptions.click()
    time.sleep(2)
    btnTrash = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Déplacer "
                                                                                         "dans la corbeille']")))
    time.sleep(1)
    btnTrash.click()
    time.sleep(1)
    btn_move = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,
                                                                               "/html/body/div[1]/div/div["
                                                                               "1]/div/div[ "
                                                                               "6]/div/div/div[1]/div/div["
                                                                               "2]/div/div/div/div/div/div/div["
                                                                               "3]/div/div/div/div[1]")))
    time.sleep(1)
    btn_move.click()
    time.sleep(7)
except NoSuchElementException:
    break

my program runs as expected. I would now like to know if it's possible for it to do one more thing:

I need my program to look for the presence of an element first and if he finds it then click on it. and if it does not find it then execute the code above

I tried to use if and else conditions but without success yet

some help would be greatly appreciated

CodePudding user response:

You can try something like:

try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "my_element"))).click()
except:
    while True:
        try:
            btnOptions = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div["
                                                                                                   "1]/div/div["
                                                                                                   "1]/div/div["
                                                                                                   "5]/div/div/div["
                                                                                                   "3]/div/div/div["
                                                                                                   "1]/div["
                                                                                                   "1]/div/div/div["
                                                                                                   "4]/div[2]/div/div["
                                                                                                   "2]/div[3]/div["
                                                                                                   "3]/div/div/div/div"
                                                                                                   "/div/div/div/div/div"
                                                                                                   "/div/div["
                                                                                                   "8]/div/div/div["
                                                                                                   "2]/div/div["
                                                                                                   "3]/div/div")))
            time.sleep(2)
            btnOptions.click()
            time.sleep(2)
            btnTrash = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Déplacer "
                                                                                                 "dans la corbeille']")))
            time.sleep(1)
            btnTrash.click()
            time.sleep(1)
            btn_move = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,
                                                                                       "/html/body/div[1]/div/div["
                                                                                       "1]/div/div[ "
                                                                                       "6]/div/div/div[1]/div/div["
                                                                                       "2]/div/div/div/div/div/div/div["
                                                                                       "3]/div/div/div/div[1]")))
            time.sleep(1)
            btn_move.click()
            time.sleep(7)
        except NoSuchElementException:
            break

You try to click your element, if ok then code will continue after the except block, if click fails means element was not there, so it will run your expected code into except block

CodePudding user response:

The cleanest way to validate element existence is to use find_elements method since in this case there will no exception thrown in case of no element found.
There are several issues should be improved in your code:

  1. Since you are clicking elements here you should use element_to_be_clickable expected_conditions, not presence_of_element_located.
  2. No need to add sleeps between actions when you are using WebDriverWait expected_conditions.
    3)You can perform click() directly on the returned web element object, no need to put it into temporary variable.
    4)Your locators should be improved, but I can't help here since I don't know what page are you working on.

So, your code can be like the following:

first_element_list = driver.find_elements(By.XPATH,"the_element_locator")
if first_element_list:
    first_element_list[0].click()
else:
    while True:
        try:
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div[2]/div/div[2]/div[3]/div[3]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div/div[2]/div/div[3]/div/div"))).click()
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Déplacer dans la corbeille']"))).click()
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,  "/html/body/div[1]/div/div[1]/div/div[6]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div[3]/div/div/div/div[1]"))).click()
        except NoSuchElementException:
            break
  • Related