简体   繁体   English

硒find_element_by_xpath返回NoSuchElementException

[英]selenium find_element_by_xpath returns NoSuchElementException

I'm working with selenium but im having problems with the find_element_by_xpath method. 我正在使用硒,但是我在find_element_by_xpath方法上遇到了问题。 Having this tag from the https://citizenportal.rld.state.nm.us/default.aspx website: https://citizenportal.rld.state.nm.us/default.aspx网站获得此标签:

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

Im trying to create a click event to click on the permits tag by doing: 我试图通过执行以下操作来创建点击事件以点击“允许”标签:

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()

However im getting this error and i cant figure it out 但是即时通讯收到此错误,我无法弄清楚

---------------------------------------------------------------------------
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)

I've tried different combinations but it keeps returning an error. 我尝试了不同的组合,但始终返回错误。 What am i doing wrong? 我究竟做错了什么?

EDIT that includes the hole span and its elements. 编辑,包括孔跨度及其元素。

<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>

Your XPath is fine. 您的XPath很好。 I prefer to use CSS selectors, eg "a[title='Permit']" , since they are faster and better supported. 我更喜欢使用CSS选择器,例如"a[title='Permit']" ,因为它们更快,更好地受支持。

The problem is that the element you want is inside an IFRAME. 问题是所需的元素在IFRAME中。

<iframe id="ACAFrame" name="ACAFrame" ... src="/Welcome.aspx">

You will need to switch into that frame and then your find will work. 您将需要切换到该框架,然后您的查找将起作用。

See this page for more details. 有关更多详细信息,请参见此页面

Anchor tags do not have a standard title attribute: https://www.w3schools.com/tags/tag_a.asp 定位标记没有标准的标题属性: https : //www.w3schools.com/tags/tag_a.asp


I suggest using: 我建议使用:

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

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

Whenever you try searching for the elements within the iframe you must have to switch the focus to the iframe that you are dealing with. 每当尝试在iframe中搜索元素时,都必须将焦点切换到要处理的iframe

Do this before searching for the elements within the iframe: 在iframe中搜索元素之前,请执行以下操作:

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

In this case, the iframe Title is : ACAFrame 在这种情况下,iframe标题为: ACAFrame

below is the updated code: 下面是更新的代码:

driver = webdriver.Chrome()
driver.get('https://citizenportal.rld.state.nm.us/default.aspx')
driver.switchTo().frame(driver.findElement(By.name('ACAFrame')));
driver.find_element_by_xpath('//a[@title="Permits"]').click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM