通过LinkText定位元素时出现NoSuchElementException('无法定位元素:研究项目',无,无)

时间:2018-07-05 21:55:49

标签: selenium firefox selenium-webdriver webdriver phantomjs

我目前有一个与PhantomJS一起使用的硒python脚本,但与Firefox无关。我收到此错误:

NoSuchElementException('Unable to locate element: Research Project', None, None)

具体地说,它在以下代码行上失败:

self.webdriver.find_element_by_link_text("Research Project").click()

我尝试了一些不走运的等待方法,例如:

WebDriverWait(self.webdriver, 10000).until(EC.presence_of_element_located((By.LINK_TEXT, "Research Project")))

我还尝试使用各种Xpath来测试链接文本是否不够具体,如果未执行任何等待,或者导致执行超时,则会导致ElementNotInteractableException。

如果有帮助,我的PhantomJS webdriver定义为:

args = [
'--ignore-ssl-errors=true',
'--ssl-protocol=any',
'--web-security=false'
]
driver = webdriver.PhantomJS(service_args=args, executable_path='/usr/bin/phantomjs', service_log_path='/tmp/ghostdriver.log')

我的Firefox网络驱动程序简单定义为:

driver = webdriver.Firefox(executable_path='/usr/local/Cellar/geckodriver/0.21.0/bin/geckodriver')

如有必要,我可以提供更多详细信息。任何帮助将不胜感激。

我要引用的对象的HTML:

<a href="#" id="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:close_tree" name="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:close_tree" onclick="RichFaces.ajax(&quot;dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:close_tree&quot;,event,{&quot;parameters&quot;:{&quot;node_id&quot;:5627697,&quot;ajaxRequest&quot;:&quot;true&quot;} ,&quot;incId&quot;:&quot;1&quot;} );return false;" class="publicationTypeLink ariaHasPopup ariaExpanded" onblur="overviewAddnew(null);unselectedDiv(this);" onfocus="overviewAddnew(this);selectedDiv(this);" onmouseout="overviewAddnew(null);unselectedDiv(this);" onmouseover="overviewAddnew(this);selectedDiv(this);" aria-describedby="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:hiddenDescriptiontree_closed" aria-haspopup="true" aria-expanded="true"><div id="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:closeTree" style="padding-left:20px;" class="unselectedDiv"><img src="/converis/javax.faces.resource/images/collapsible_panel_triangle_state_expanded.png.xhtml?ln=intern" alt=""><span style="padding-left:15px;">Research Project </span></div><span id="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:hiddenDescriptiontree_closed" style="display: none;">Research Project</span></a>

1 个答案:

答案 0 :(得分:0)

相关的HTML有助于确定Locator Strategy。不过,根据您的代码试用,在尝试调用click()方法时应继续前进,因此,除了使用presence_of_element_located()之外,还需要使用 element_to_be_clickable() ,如下所示:

  • LINK_TEXT

    WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Research Project"))).click() 
    
  • PARTIAL_LINK_TEXT

    WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Research Project"))).click()
    
  • XPATH

    WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Research Project')]"))).click()
    

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC