Selenium Webdriver在下一个链接中查找元素 [英] Selenium webdriver finding element in next link

查看:64
本文介绍了Selenium Webdriver在下一个链接中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开主页(主页),然后使用click()函数转到登录页面,现在我想在此页面中查找元素,我该怎么办?

open main(home) page and then go to login page using click() function , now i want to find element in this page how could I?

这是我的代码...

import unittest,time,re
from selenium import webdriver
from selenium import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        #self.selenium = selenium("localhost", 4444, "*firefox","http://www.google.com/")
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("https://bitbucket.org/")
        elem = driver.find_element_by_id("user-options")
        elem = elem.find_element_by_class_name("login-link")
        elem.click()
        print "check"

        #elem = WebDriverWait(driver, 30).until(EC.elementToBeVisible(By.name("username")));
        #elem.send_keys("my_username@bitbucket.org")

        user_name_field = driver.find_element_by_id('id_username')
        password_field = driver.find_element_by_id('id_password')

        user_name_field.send_keys('your_user_name')
        password_field.send_keys('your_password')

    def tearDown(self):
        pass
        #self.driver.close()

if __name__ == "__main__":
    unittest.main()

我收到此错误(文件名为python_org_search.py​​)

I got this error (file name python_org_search.py)

E
======================================================================
ERROR: test_search_in_python_org (__main__.PythonOrgSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "python_org_search.py", line 25, in test_search_in_python_org
    user_name_field = driver.find_element_by_id('id_username')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 197, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"id_username"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpKo1TXx/extensions/fxdriver@googlecode.com/components/driver_component.js:8860)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmpKo1TXx/extensions/fxdriver@googlecode.com/components/driver_component.js:8869)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpKo1TXx/extensions/fxdriver@googlecode.com/components/command_processor.js:10831)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpKo1TXx/extensions/fxdriver@googlecode.com/components/command_processor.js:10836)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpKo1TXx/extensions/fxdriver@googlecode.com/components/command_processor.js:10778) 

----------------------------------------------------------------------
Ran 1 test in 30.994s

FAILED (errors=1)

我也尝试了其他解决方案,但仍然存在相同的错误.

I tried other solutions as well but still same error .

推荐答案

您最好尝试通过其ID来检索唯一的元素,例如用户名字段和密码字段. name可能不是唯一的并且具有误导性.

You better should try to retrieve unique elements like the username field and password field by it's id. The name might not be unique and misleading.

尝试以下find_element_by_id('id_username')find_element_by_id('id_password').

有时您需要等待一段时间,以使浏览器呈现页面 并检索所有内容-等待一段时间会很有用, 例如在深入了解元素之前三秒钟.

Sometimes you need to wait some time that your browser renders the page and retrieves all contents - so it can be useful to wait some time, e.g. three seconds before you look deeper for elements.

代码

import time

time.sleep(3)
user_name_field = driver.find_element_by_id('id_username')
password_field = driver.find_element_by_id('id_password')

user_name_field.send_keys('your_user_name')
password_field.send_keys('your_password')

password_field.send_keys(Keys.RETURN)
...

此外,我建议您对不同的字段使用不同的变量名称.变量elem可能会导致难以解决的错误.

Moreover, I recommend you to use different variables names for the different fields. The variable elem could lead to difficult bugs.

上述三秒钟的等待时间可能是不可靠的-确保某些等待时间的更结构化的方法是通过implicitly_wait告诉它您的driver:

Three seconds of waiting time like mentioned above can be unreliable - a more structured way of assuring some waiting time is it to tell it your driver through implicitly_wait:

driver = webdriver.Firefox()
driver.implicitly_wait(30) # maximally wait 30 seconds before raising an exception.

# ...
# go to the page etc.

驾驶员有两种等待时间的方法:

There are two ways of waiting time a driver can do:

  • 明确等待:驱动程序等待此时间,然后再执行下一步.
  • 隐式等待:如果您查找某个元素,则驱动程序会在此事件引发异常之前进行搜索.
  • Wait explicitly: The driver waits this time before executing every next step.
  • Wait implicitly: If you look for an element the driver searches for this time before he raises an exception.

有关更多详细信息,请参见文档: http://www.seleniumhq. org/docs/04_webdriver_advanced.jsp#implicit-waits

For more details see the documentation: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits

这篇关于Selenium Webdriver在下一个链接中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆