Selenium Webdriver - 如何选择元素

问题描述 投票:0回答:1

在下面的页面摘录中,我需要能够单击“应用程序配置文件” 但无论我尝试什么,我都无法让它发挥作用。 我不断收到 OpenQA.Selenium.NoSuchElementException。

<body>
   <div id="main-page">
       <div class="page">                       
            </div>
                <div id="page-header" class="page-header">                    
                    <div id="page-header-navbar" class="page-header-navbar">                        
                        <div class="page-header-contents">
                            <div class="page-header-navbar-left">
                                <div class="nav-menus">
                                    <div class="nav-menu" data-item="dashboard">
                                        <div class="nav-menu-header selected -js-header">
                                            <i class="nav-menu-icon fa fa-tachometer"></i> <span
                                                 class="nav-menu-header-label">Dashboard</span>
                                        </div>
                                    </div>                                    
                                    <div class="nav-menu" data-item="policy">
                                        <div class="nav-menu-header -js-header">
                                            <i class="nav-menu-icon fa fa-sitemap"></i><span
                                                 class="nav-menu-header-label">App Profiles</span>
                                        </div>
                                    </div>                                    
                                </div>
                            </div>                          
                        </div>
                    </div>          
                </div>
        </div>
</body>

这些已经尝试过:
IWebElement element = driver.FindElement(By.XPath("//div[@class='nav-menu-header-label' and text()='App Profiles']"));
IWebElement appProfilesElement = driver.FindElement(By.XPath("//span[text()='应用程序配置文件']"));

selenium-webdriver browser webdriver microsoft-edge browser-automation
1个回答
0
投票

先尝试等待:

from selenium.webdriver.support.ui import WebDriverWait
# wait for 10 seconds
wait = WebDriverWait(driver, 10)
xpath = "//div[@class='nav-menu-header-label' and text()='App Profiles']"
element = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
element.click()
© www.soinside.com 2019 - 2024. All rights reserved.