python 和 Selenium 的新手以及相关的编程。
我正在尝试自动化 selenium 以点击特定链接。在这种情况下,我希望它点击与链接文本“B”关联的链接:
<li><a href="javascript:__doPostBack('ctl00$IndexControl1','B')">B</a>
在这个网站上:
我正在使用这段代码:
def get_single_link_using_find_elements_by_link_name(url, link_name):
driver = webdriver.Firefox()
driver.get(url)
driver.implicitly_wait(10)
time.sleep(20)
element = driver.find_element_by_link_text(link_name)
element.click()
我添加了一些等待条件,因为我认为问题可能是渲染问题,但它们没有帮助。
我收到以下错误:
Traceback (most recent call last):
File "C:\Python27\programs\selenium commands.py", line 50, in <module>
get_single_link_using_find_elements_by_link_name(url, link_name)
File "C:\Python27\programs\selenium commands.py", line 47, in get_single_link_using_find_elements_by_link_name
element = driver.find_element_by_link_text(link_name)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 237, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 671, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 156, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"B"}'
奇怪的是,相同的代码适用于以下网址,它是同一网站的一部分: http ://www.lw.com/people?searchIndex=A
有任何想法吗?