selenium find_element_by_xpath返回NoSuchElementException

时间:2017-04-14 16:26:51

标签: python selenium

我正在使用selenium但是我遇到了find_element_by_xpath方法的问题。 从https://citizenportal.rld.state.nm.us/default.aspx网站获取此标记:

<a title="Permits" href="javascript:void(0);" module="Permits">Permits</a>

我尝试创建一个点击事件,点击许可标签:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://citizenportal.rld.state.nm.us/default.aspx')

driver.find_element_by_xpath('//a[@title="Permits"]').click()

然而,我得到这个错误,我无法弄清楚

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-25-ecbedf623e4f> in <module>()
      6 driver.get('https://citizenportal.rld.state.nm.us/default.aspx')
      7 
----> 8 driver.find_element_by_xpath('//a[@title="Permits"]').click()
      9 

C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.pyc in find_element_by_xpath(self, xpath)
    304             driver.find_element_by_xpath('//div/td[1]')
    305         """
--> 306         return self.find_element(by=By.XPATH, value=xpath)
    307 
    308     def find_elements_by_xpath(self, xpath):

C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.pyc in find_element(self, by, value)
    782         return self.execute(Command.FIND_ELEMENT, {
    783             'using': by,
--> 784             'value': value})['value']
    785 
    786     def find_elements(self, by=By.ID, value=None):

C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.pyc in execute(self, driver_command, params)
    247         response = self.command_executor.execute(driver_command, params)
    248         if response:
--> 249             self.error_handler.check_response(response)
    250             response['value'] = self._unwrap_value(
    251                 response.get('value', None))

C:\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.pyc in check_response(self, response)
    191         elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
    192             raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
--> 193         raise exception_class(message, screen, stacktrace)
    194 
    195     def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@title="Permits"]"}
  (Session info: chrome=57.0.2987.133)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.3.9600 x86_64)

我尝试过不同的组合,但不断返回错误。我做错了什么?

包含孔跨度及其元素的编辑。

<span style="display:inline-block;" id="span_tab_1">                       
    <table role="presentation" tag="navbar" border="0" cellspacing="0" cellpadding="0" class="tab_bar_table ACA_Nowrap">                           
        <tbody>
            <tr>
                <td class="ACA_ItemLeft ACA_LeftOff"></td>                                  
                <div>                                           
                    <a title="Permits" href="javascript:void(0);" module="Permits">Permits</a>                                       
                </div>
                <td class="ACA_ItemCenter ACA_ItemCenter_FontSize ACA_CenterOff">   
                </td>                                   
                <td class="ACA_ItemRight ACA_RightOff"></td>                           
        </tr>                       
 <      /tbody>
    </table>                    
 </span>

3 个答案:

答案 0 :(得分:1)

你的XPath很好。我更喜欢使用CSS选择器,例如<iframe id="ACAFrame" name="ACAFrame" ... src="/Welcome.aspx"> ,因为它们更快,更好地得到支持。

问题是你想要的元素在IFRAME中。

app

您需要切换到该框架,然后您的查找才能正常工作。

有关详细信息,请参阅this page

答案 1 :(得分:0)

锚标记没有标准的标题属性:https://www.w3schools.com/tags/tag_a.asp


我建议使用:

driver.find_element_by_link_text('Permits').click()

per:http://selenium-python.readthedocs.io/locating-elements.html#locating-hyperlinks-by-link-text

答案 2 :(得分:0)

每当您尝试搜索iframe中的元素时,您必须将焦点切换到您正在处理的iframe

在搜索iframe中的元素之前执行此操作:

var light = new THREE.PointLight( 0xffffff, 1 );

在这种情况下,iframe标题为: ACAFrame

下面是更新的代码:

driver.switchTo().frame(driver.findElement(By.name("iframeTitle")));