简体   繁体   中英

Selenium : How to wait then click?

I'm using selenium for automation and i want to click in each one of the <ul> elements then wait before clicking again in the element. This is my code but it doesn't seem to be the solution :

def navBar():
 driver=setup()
 navBar_List = driver.find_element_by_class_name("nav")
 listItem = navBar_List.find_elements_by_tag_name("li")
 for item in listItem :
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.TAG_NAME,"li")))
    item.click()

Here the HTLM code :

<ul class="nav navbar-nav"> 
                <li tabindex="0">
                        <a class="h">
                            <div class="icon-left-navbar">
                                 ...
                            </div>
                        </a>
                    </li>
                    <li tabindex="0">
                        <a class="h">
                            <div class="icon-left-navbar">
                                 ...
                            </div>
                        </a>
                    </li>
                    <li tabindex="0">
                        <a class="h">
                            <div class="icon-left-navbar">
                                 ...
                            </div>
                        </a>
                    </li>
                </ul>

Define your li with .find_elements .

Use xpath for recognize them : //ul[@class='nav navbar-nav']//li .

With loop you can utilize increment to wait each li . I'm imagine it will produce like below:

(xpath)[1]
(xpath)[2]
etc...

And try the following code:

listItem = WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH,"//ul[@class='nav navbar-nav']//li")))

for x in range(1, len(listItem)+1):
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//ul[@class='nav navbar-nav']//li)[" +str(x) +"]"))).click()

Thread.sleep(100)是一个选项吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM