python测试中使用selenium按id获取元素

2024-06-16 09:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

在我下面的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> 

我想选择注销按钮,它实际上出现在导航栏的选择器中。在

我试着通过名字,身份证等来获取元素,但是没有成功。在

^{pr2}$

我显式地在注销的a 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) 

Tags: comjscomponentsextensionslitmpatclass
2条回答

Selenium / Firefox: Command ".click()" doesn't work with a found element得到这个

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选择支持(类selenium.webdriver.support.选择。选择(webelement))?在

我不太了解Python,所以我的例子是用Java编写的:

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

相关问题 更多 >