3

All, I'm needing a little assistance with Selenium waits. I can't seem to figure out how to wait for an element to be ready.

The element that I am needing to wait I can locate and click using my script via the code below...

CreateJob = driver.find_element_by_xpath(".//*[@id='line']/div[1]/a")

or

CreateJob = driver.find_element_by_partial_link_text("Create Activity")

I'm needing to wait for this element to be on the page and clickable before I try to click on the element.

I can use the sleep command, but I have to wait for 5 seconds or more and it seems to be unreliable and errors out 1 out of 8 times or so.

I can't seem to find the correct syntax to use.

the HTML code for this is below.

<document>
<html manifest="https://tddf/index.php?m=manifest&a=index">
<head>
<body class="my-own-class mozilla mozilla48 mq1280 lt1440 lt1680 lt1920 themered" touch-device="not">
<noscript style="text-align: center; display: block;">Please enable JavaScript in your browser settings.</noscript>
<div id="wait" style="display: none;">
<div id="processing" class="hidden" style="display: none;"/>
<div id="loading" class="hidden" style="display: none;"/>
<div id="loadingPartsCatalog" class="hidden"/>
<div id="panel">
<div id="top-toolbar" class="hidden" style="display: block;">
<div id="commands-line" class="hidden" style="display: block;">
<div id="line">
<div class="action-link">
<a class="tap-active" href="#m=activity/a=set" action_link_label="create_activity" component_gui="action" component_type="action">Create Activity</a>
</div>
<div class="action-link">
<div class="action-link">
<div class="action-link">
</div>
<div id="commands-more" style="display: none;">
<div id="commands-list" class="hidden">
</div>
<div id="provider-search-bar" class="hidden center"  

2 Answers 2

7

Here is a link to the 'waiting' section of the Python Selenium docs: http://selenium-python.readthedocs.io/waits.html#explicit-waits

You wait should look like this:

element = WebDriverWait(driver, 10).until(
    EC.visibility_of((By.XPATH, ".//*[@id='line']/div[1]/a"))
)
Sign up to request clarification or add additional context in comments.

4 Comments

That worked. Any Idea how I can use this based on the text of the element?
element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.LINK_TEXT, "Create Activity")) )..
@th2112 Saurabh is correct, there is also a By.PARTIAL_LINK_TEXT. selenium-python.readthedocs.io/locating-elements.html
Both Worked! Thanks, I was just having problems with the syntax of it.... still learning...
0

I find this to be the easiest:

driver.implicitly_wait(10) 

Where it waits for up to 10 seconds before the script might crash if expected conditions aren't met. I think it's better than always checking for the visibility of, the clickability of, or whatever it is about the element. Less effective and more error prone, however. So it would depend more on why you use selenium.

It also lets me cut down on try/except statements in my selenium scripts, and since I've found out about this I've reduced many time.sleep() functions as well.

Comments

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.