简体   繁体   English

在Python测试中使用Selenium通过ID获取元素

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

In my html code below: 在下面的我的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> 

I want to select the logout button which is actually appears in a selector in my nav bar. 我想选择注销按钮,该按钮实际上显示在导航栏中的选择器中。

I tried getting element by name , id etc but nothin worked out. 我试图通过名称,id等获取元素,但没有得到解决。

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

I explicitly added id in a href link for logout , but could not grab the element. 我在href链接中明确添加了id以便注销,但是无法获取该元素。 Any suggestion how can I get the logout element 任何建议如何获取注销元素

I get below exception: 我得到以下异常:

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) 

Got this from Selenium / Firefox: Command ".click()" doesn't work with a found element 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")

Have you tried to use Select from UI Support (class selenium.webdriver.support.select.Select(webelement))? 您是否尝试过使用UI支持中的Select(类selenium.webdriver.support.select.Select(webelement))?

I don't known Python well so my example is in Java: 我不太了解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