簡體   English   中英

在Python測試中使用Selenium通過ID獲取元素

[英]Using selenium to get element by id in python testing

在下面的我的html代碼中:

<div class="collapse navbar-collapse" id="b-menu-1">
                    <ul class="nav navbar-nav navbar-right">


                        <li><a href="/accounts/login/">Sign In</a></li>
                        <li><a href="/accounts/signup/">Sign Up</a></li>


                        {% if request.user.is_authenticated or logged_in %}
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                                <span class="glyphicon glyphicon-user"></span><b class="caret"></b></a>

                            <ul class="dropdown-menu">
                                <li><a href="/accounts/manageAccount/">Manage Account</a></li>
                                <li><a href="/accounts/createProfile/">create profile</a></li>
                                <li><a href="/accounts/viewProfile/">view profile</a></li>
                                <li><a href="/accounts/editProfile/">edit profile</a></li>  
                                <li><a href="/accounts/logout/" id="logout">Log out</a></li>
                            </ul>
                        </li>


                        <li data-toggle="modal" data-target="#my-modal-box" class="active">
                            <a href="#"><span class="glyphicon glyphicon-search"> Search</a></li>
                    </ul>
                </div> 

我想選擇注銷按鈕,該按鈕實際上顯示在導航欄中的選擇器中。

我試圖通過名稱,id等獲取元素,但沒有得到解決。

elem2 = self.driver.find_element_by_id("logout")
elem2.send_keys(Keys.RETURN)

我在href鏈接中明確添加了id以便注銷,但是無法獲取該元素。 任何建議如何獲取注銷元素

我得到以下異常:

NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"logout"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/driver_component.js:8905)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/driver_component.js:8914)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10884)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10889)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10831) 

Selenium / Firefox中獲得了此命令:命令“ .click()”不適用於找到的元素

def javascript_manual_click(driver, element_id):
    we = driver.find_element_by_id(element_id)
    driver.execute_script("arguments[0].click();", we)

javascript_manual_click("logout")

您是否嘗試過使用UI支持中的Select(類selenium.webdriver.support.select.Select(webelement))?

我不太了解Python,所以我的示例在Java中:

    WebElement dropDownMenu = webDriver.findElement(By.xpath("//ul[@class='dropdown-menu']");
    Select logout = new Select(dropDownMenu);
    logout.selectByVisibleText("Log out");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM