Search code examples
pythonselenium-webdriverxpath

Selenium, python, findelement, Xpath methods impossible to locate or click an element on my page


I have no way to locate an element on a page after successfully logging to the website.

My objective is to click on a button in order to participate to a proposed activity. The button appears only after midnight and I do not want to stay in front of my PC to click on the participate button.

Using the below python script, I can already log and connect to the website :

# find username field and send the username itself to the input field
browser.find_element("id", "login").send_keys(username)
# find password input field and insert password as well
browser.find_element("id", "motdepasse").send_keys(password)
# click login button
browser.find_element(By.CLASS_NAME, "submit").click()

The new page appears but I have now way to click on the participate button

Here is the page code

<li class="td" style="width:5%;">
    ul class="BtnsIco">
                    <li class="ajaxOpen user" data-url="_Programme_lstparticipants&amp;id=3593" title="Liste des participants" data-titre="Liste des participants"></li> <li class="bouton partAdd" data-url="_Programme_inscription&amp;id=3593" title="S'inscrire" data-titre="S'inscrire"></li> 
    </ul>
</li>

I tried several method to click the class "bouton partAdd" but no way to find a XPATH method which detects the button as I did on the fist page connexion.

try:
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable(By.PARTIAL_LINK_TEXT, 'inscrire'))
    PartAdd_link = browser.find_element(By.PARTIAL_LINK_TEXT, 'inscrire')    
    print(PartAdd_Link)
except:
    print("Could not get PartAdd_Link")
    pass
try:
    WebDriverWait(browser, 20).until(EC.element_to_be_clickable(By.CLASS_NAME, 'bouton partAdd'))
    content = browser.find_element(By.CLASS_NAME, 'bouton partAdd')
    print(content.CLASS_NAME)
except:
    print("Could not get Class_Name")
    pass

My code always rises exception. I tried other methods without success. I added WebdriverWait security but it does not have any effect on the result.

I have no experience in web programming so if anyone could give me a way to find or understand the problem I would be grateful.

Thank you in advance


Solution

  • If your use case is to click a button that appears post midnight. My approach would be to use the datetime library in python.

    from datetime import datetime
    import time
    
    def midnightChecker(day):
        while day+1 != datetime.now().day:
            time.sleep(5)
            print(f'{23-datetime.now().hour} hour {59-datetime.now().minute} minutes and {59-datetime.now().second} seconds left till midnight')
            continue
    midnightChecker(datetime.now().day)
    

    This loop will break at midnight. You can add this checker. Try clicking the button post this, If you are still getting the error, kindly share the error message you are getting.

    In your exception block i.e the except: block, instead of writing just except: write

    except Exception as e:
           print(e)
    

    This will print what exception is getting raised.