【问题标题】:Python selenium - get xPath of element and access itPython selenium - 获取元素的 xPath 并访问它
【发布时间】:2017-03-23 06:13:32
【问题描述】:

我正在使用 Python 和 Selenium 来抓取网页,在某些情况下,我无法让它工作, *

我想访问带有文本“PInt”的元素,这是下面代码中的第二个链接。 它的 xPath(从开发者控制台复制)是://[@id="submenu1"]/a[2]

      <div id="divTest" onscroll="SetDivPosition();" style="height: 861px;">
            <div class="menuTitle" id="title1">
                <a href="#" onclick="toggle(1);"> </a>
            </div>
            <div class="subtitle" id="submenu1">    
                <img src="images/spacer.gif" border="0" width="2px" height="12px">
                <a href="#" class="NormalBlueSmall" onclick="clickItem('area/search/mov/mov2','mov');">Mov</a><br>
                <img src="images/spacer.gif" border="0" width="2px" height="12px">
                <a href="#" class="NormalBlueSmall" onclick="clickItem('area/con/ExtInt/extInt','pIint');">PInt</a><br>
                <img src="images/spacer.gif" border="0" width="2px" height="12px">
                <a href="#" class="NormalBlueSmall" onclick="clickItem('GoToNew.asp?link=asw_cnt/SmanSwif.aspx','SMAN/SWIF');">SWAM / SWIF</a><br>
            </div>


...

我的代码片段是:

try:
    res = driver.find_elements_by_link_text('PInt')
    print("res1:{}".format(res))

    res = driver.find_element(By.XPATH,'//*[@id="submenu1"]/a[3]')
    print("res:{} type[0]:{}".format(res,res[0]))
    itm1 = res[0]
    itm1.click()

我得到错误:

无法定位元素: {"method":"xpath","selector":"//*[@id="submenu1"]/a[2]"}

我的问题是,我怎样才能获得元素的正确 xPath 或任何其他方式来访问元素?

更新: 这可能很重要,问题在于 Message: invalid selector: Unable to locate an element with the xpath expression(我已经尝试了所有建议的解决方案)可能是在之前在网页(用户+密码)中进行身份验证之后,一切正常。 我注意到登录后的 url driver.current_url 是静态的(asp 页面)。 这也是我试图在框架集和 frame

中访问的部分

html &gt; frameset &gt; frameset &gt; frame:nth-child(1)

【问题讨论】:

  • 元素是否在 iframe 中?您可能还需要等待它可用。
  • 可以分享一下网址吗?
  • 试试这个 xpath //div[@id='submenu1']//a[2]
  • @JeffC 它不在 iFrame 内,而是在
  • 你需要切换到框架。您应该能够找到一些示例代码。试试看它是否有效。

标签: python selenium xpath web-scraping


【解决方案1】:

感谢@JeffC 为我指明了正确的方向。

由于页面有一些框架,我首先通过切换到正确的框架来访问元素(使用 xPath) 然后访问元素。

driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_xpath('html / frameset / frameset / frame[1]'))

driver.find_element_by_xpath("//a[contains(text(),'PInt')]").click()

顺便说一句,如果你想从 crontab 运行脚本,你需要设置一个显示:

30 5 * * * export DISPLAY=:0; python /usr/.../main.py

【讨论】:

    【解决方案2】:

    要查看使用 selenium 选择元素的所有方法的完整列表,您可以在 documentation 中阅读所有相关信息。

    使用 xpath:

    res = driver.find_element_by_xpath(u'//*[@id="submenu1"]/a[2]')
    

    使用 css 选择器:

    res = driver.find_element_by_css_selector('#submenu1 a:nth-of-type(2)')
    

    【讨论】:

    • @Algina - 我用我的 css 选择器修复了一个错误。试试看。关于 xpath,您得到的错误意味着具有给定 xpath 的元素不存在或 xpath 不正确。
    • 谢谢欧内尔。这是同样的错误:Message: no such element: Unable to locate element: {"method":"css selector","selector":"#submenu1 a:nth-of-type(2)"} 也许该页面以某种方式受到保护,不受这些东西的影响......这是一个“安全”页面..
    • 再尝试一件事。选择并单击主菜单,当菜单项出现时,选择并单击要单击的元素。让我知道这是否有效 - @Algina
    【解决方案3】:

    尝试使用以下任何 xpath。有时自动生成的 xpath 不起作用。

    //a[contains(text(),'PInt')]
    or
    //div[@id='submenu1']//a[contains(text(),'PInt')]
    

    另外我建议你在点击上面的链接之前设置一些等待时间,以防上面的 xpath 不起作用

    【讨论】:

    • 也试试这个//div[@id='submenu1']//following::a[contains(text(),'PInt')]
    • 不幸的是,任何解决方案都有效。不确定如何,但可能是页面阻止了一些抓取?
    【解决方案4】:

    在 chrome 中查找 xPath:

    1. 右键单击所需的元素
    2. 检查,这将打开开发人员窗口并突出显示选定的元素。
    3. 右键单击突出显示的元素并选择复制 > 通过 xpath 复制

    这里列出了定位元素Locating Elements的所有不同方法

    【讨论】:

    • 是的,这就是我所做的并 git xPath: //[@id="submenu1"]/a[2] 这似乎是无效的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多