我有这样的HTML代码:
<div class="links nopreview"><span><a class="csiAction" href="/WebAccess/home.html#URL=centric://REFLECTION/INSTANCE/_CS_Data/null">Home</a></span> • <span><span><a class="csiAction" href="/WebAccess/home.html#URL=centric://SITEADMIN/_CS_Site">Setup</a></span> • </span><span><a title="Sign Out" class="csiAction csiActionLink">Sign Out</a></span></div>
我想单击文本为 Home 的链接。由于此Home链接在登录后出现,因此我具有如下代码:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import re browser = webdriver.Firefox() # Get local session of firefox browser.get("http://myServer/WebAccess/login.html") # Load App page elem = browser.find_element_by_name("LoginID") # Find the Login box elem.send_keys("Administrator") elem = browser.find_element_by_name("Password") # Find the Password box elem.send_keys("Administrator" + Keys.RETURN) #try: elem = browser.find_element_by_link_text("Home") elem.click()
登录之前的部分效果很好。但是最后一行是有问题的
elem = browser.find_element_by_link_text("Home")
从HTML代码可以看到,它引发了此NoSuchElementException的Home链接。
raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"Home"}'
请问关于我在做什么错的任何指导?
您是否尝试过为此添加隐式等待,以使其等待而不是快速运行。
from selenium import webdriver from selenium.webdriver.common.keys import Keys import re browser = webdriver.Firefox() # Get local session of firefox browser.implicitly_wait(10) #wait 10 seconds when doing a find_element before carrying on browser.get("http://myServer/WebAccess/login.html") # Load App page elem = browser.find_element_by_name("LoginID") # Find the Login box elem.send_keys("Administrator") elem = browser.find_element_by_name("Password") # Find the Password box elem.send_keys("Administrator" + Keys.RETURN) #try: elem = browser.find_element_by_link_text("Home") elem.click()
该implicitly_wait调用使浏览器开始轮询,直到该项目在页面上并且可见与之交互为止。
implicitly_wait