Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python测试中使用selenium按id获取元素_Python_Selenium_Python Unittest - Fatal编程技术网

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

在python测试中使用selenium按id获取元素,python,selenium,python-unittest,Python,Selenium,Python Unittest,在下面我的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>

在下面我的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> 
我在一个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) 
NoSuchElementException:Message:u'无法定位元素:{“方法”:“id”,“选择器”:“logout”}';堆栈跟踪:
在FirefoxDriver.prototype.findelement内部(file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/driver_component.js:8905)
在FirefoxDriver.prototype.findElement(file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/driver_component.js:8914)
在DelayedCommand.prototype.executeInternal\uh处(file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10884)
在DelayedCommand.prototype.executeInternal\u(file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10889)
在DelayedCommand.prototype.execute/<(file:///tmp/tmpPHM5S7/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)


您是否尝试过使用Select from UI支持(类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");

尝试
elem2。单击()
?如果不起作用,请检查我的编辑以查看异常。这是模板页吗?Ie:
{%if request.user.is\u已验证或登录%}
?可能在服务页面上尝试此测试?尝试打印self.driver.page\u source?当找不到元素时,通常不会出现NoTouchElementException,您确定打开的页面正确吗。首先尝试打印页面源代码并检查itelem2.send_keys(keys.RETURN),如果它是一个按钮,它实际上会单击。
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")
    WebElement dropDownMenu = webDriver.findElement(By.xpath("//ul[@class='dropdown-menu']");
    Select logout = new Select(dropDownMenu);
    logout.selectByVisibleText("Log out");