tangyuan
V2EX  ›  问与答

html页面中的xpath如何提取并使用在selenium中啊

  •  
  •   tangyuan · Mar 16, 2013 · 4233 views
    This topic created in 4980 days ago, the information mentioned may be changed or developed.
    在chrome中安装了XPath Helper的插件,希望提取html页面中的一个img图像,随后通过selenium (python) 选中该img按钮并click()

    XPath Helper返回的结果是:
    /html[@class='section_header_html_no_scroll']/body[@class='section_header_body_no_scroll']/div[@id='u_knowledge_part.form_scroll']/table[1]/tbody/tr/td/form[@id='u_knowledge_part.do']/span[@id='section_tab.f0fdfd492194d000cde2641d8d46d13f']/span[@id='spacer_section.f0fdfd492194d000cde2641d8d46d13f']/span[@id='f0fdfd492194d000cde2641d8d46d13f']/table[@class='wide']/tbody/tr[1]/td[@class='vsplit'][1]/table[@class='vsplit_bottom_margin']/tbody/tr[@id='element.u_knowledge_part.roles']/td[@class='input_controls']/table/tbody/tr/td[1]/span[@id='u_knowledge_part.roles_edit']/table/tbody/tr[2]/td[@class='bodySmall']/table/tbody/tr[1]/td/a/img/@src

    超复杂,直接用find_element_by_xpath果然就报错了,请问我是否该如何处理下这个xpath才能行呢?

    >>> a = driver.find_element_by_xpath("/html[@class='section_header_html_no_scro
    ll']/body[@class='section_header_body_no_scroll']/div[@id='u_knowledge_part.form
    _scroll']/table[1]/tbody/tr/td/form[@id='u_knowledge_part.do']/span[@id='section
    _tab.f0fdfd492194d000cde2641d8d46d13f']/span[@id='spacer_section.f0fdfd492194d00
    0cde2641d8d46d13f']/span[@id='f0fdfd492194d000cde2641d8d46d13f']/table[@class='w
    ide']/tbody/tr[1]/td[@class='vsplit'][1]/table[@class='vsplit_bottom_margin']/tb
    ody/tr[@id='element.u_knowledge_part.roles']/td[@class='input_controls']/table/t
    body/tr/td[1]/span[@id='u_knowledge_part.roles_edit']/table/tbody/tr[2]/td[@clas
    s='bodySmall']/table/tbody/tr[1]/td/a/img/@src")
    3 replies  •  1970-01-01 08:00:00 +08:00
    vibbow
    vibbow
    Mar 17, 2013
    1
    我去,XPath不用写那么深的啊
    //*[@class='bodySmall']/table/tbody/tr[1]/td/a/img/@src
    你可以试试这样写能不能找到唯一结果
    tangyuan
    tangyuan
    OP
    Mar 17, 2013
    3
    感谢,我后来用src属性值成功实现了定位
    "//img[@src='images/arrow_rt.gifx?v=2']"
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2914 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:57 · PVG 18:57 · LAX 02:57 · JFK 05:57
    ♥ Do have faith in what you're doing.