0

i create a small program in python using selenium module.

here is my code :

while True:
try:
    btnOptions = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/...."))).click()
    firstElement = driver.find_elements(By.XPATH, "//*[text()='Click']")
    if not firstElement:
        tnTrash = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[text()='Move']"))).click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
                                                                    "/html/body/..."))).click()
    else:
        firstElement.click()
except NoSuchElementException:
    break

the loop should do:

Step 1 : find the BtnOption button -> btnOptions = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/...."))).click()

the menu opens, in this menu there can be either elemnt1 or element2; never both at the same time. for example,

element1 is : 
WebDriverWait(driver, 10).until(driver.find_elements(By.XPATH, "//*[text()='element1']"))
element2 is : 
WebDriverWait(driver, 10).until(driver.find_elements(By.XPATH, "//*[text()='element2']"))

step 2 : if element 1 is found, then we click on it and restart the loop at the beginning. if it is element2 that is found, click on it and restart the loop at the beginning.

when it's a post to delete, element1 appears in the drop-down menu, if it's a profile photo, element2 appears in the drop-down menu.

for the moment my program continues as long as it finds element1, if it is element2 which is present it stops with an error telling me that element1 was not found

I thought of that but it still doesn't work :

while True:
try:
    btnOptions = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div..."))).click()
    WebDriverWait(driver, 20).until(driver.find_element(By.XPATH("//*[text()='move to trash']"))).click() or WebDriverWait(driver, 20).until(driver.find_element(By.XPATH("//*[text()='Do not show in profile']"))).click()
except NoSuchElementException:
    break

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you its helping :) i'm trying right now
0

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

17 Comments

Thank you so much for this learning session, very interesting.
i tried to improve a bit my code, but it s not working as expected yet. i need it to check if element1 is located, if yes do some action, if not ignore and search for elemnt2, if located do some actions. for now all i try is not good because it stop one time because element1 is not located or element2 etc etc
II edited the post with the new code. the loop does not repeat itself yet, I do not understand where is my mistake
Can you share a link to the page you working on? We need that for minimal debugging.
it is facebook on my own personal account
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.