【问题标题】:Selenium python click on span classSelenium python点击span类
【发布时间】:2016-01-28 09:31:21
【问题描述】:

我正在尝试自动化 Intranet 站点。源代码是一堆表格,由数百个代码组成,如下所示:

<span id="GridView1_ctl51_lblCaseType"></span>
</td><td nowrap="nowrap">
<span class="fakeLink" title="Edit case" onmouseenter="this.focus()" onclick="editCase('286658')"> 286658</span>
</td><td nowrap="nowrap">1360428-0000</td><td align="center" style="color:Red;">
<span id="GridView1_ctl51_Label2">13,0</span>
</td><td align="center">
<span id="GridView1_ctl51_Label2">8,8</span>
</td><td align="center">
<span id="GridView1_ctl51_Label9">R</span>
</td><td>
<span id="GridView1_ctl51_Label6">Waiting for info fr. suppl.</span>
</td><td>
<span id="GridView1_ctl51_Label11" title="1360428-0000 Condensate Cup&lt;br/>Price is missing, please update price&lt;br/>(enquiry by Name 11/1)" class="vtip">1360428-0000 Co</span>
</td><td>
<span id="GridView1_ctl51_Label12" title="Please source" class="vtip">Please source</span>
</td><td>
<span id="GridView1_ctl51_Label7">Fname Lname</span>
</td><td>
<span id="GridView1_ctl51_Label1"></span>
</td><td>
<span id="GridView1_ctl51_Label8">Price or delivery time missing</span>
</td><td>
<span id="GridView1_ctl51_Label3">A21</span>
</td><td>
<span id="GridView1_ctl51_Label10">TS6</span>
</td><td>
<span id="GridView1_ctl51_Label5">FName Lname</span>
</td><td>&nbsp;</td>

我需要获取部件号 1360428-0000 并单击“editCase (286658)”。我有两个问题:

  1. 使用部件号 1360428-0000 作为参考以查找案例号 286658
  2. 用xpath模仿点击286658?无法让它工作。

我尝试使用以下代码找出问题 #2,但没有成功:

def IeTest():
    driver = webdriver.Ie("C:\\Python34\\IEDriverServer.exe")
    driver.get("http://intranet.company.com")
    time.sleep(3)
    print(driver.title)
    elem = driver.find_element_by_xpath('//span[contains(@onclick,"286658")]')
    elem.click()
IeTest()

感谢任何帮助。我也必须使用IE。

【问题讨论】:

    标签: python internet-explorer selenium xpath intranet


    【解决方案1】:

    这是一种可能的 XPath,可以通过部件号 "1360428-0000" 找到目标 span

    //td[text()='1360428-0000']/preceding-sibling::td[1]/span[@title='Edit case']
    

    xpathtester.com demo

    【讨论】:

    • 最后真的需要 [1] 吗?
    • @Newcomer 不是真的,因为td 中只有一个span。我的意思是在td 之后,就像在更新的 XPath 中一样
    • 解决方案检查源 sn-p,但不检查完整源。我不是程序员/网页设计师,但我认为我正在处理的代码来自地狱。这是一个大框架,包含有关每个部分的所有信息(10k+ 代码)。我上传了更大的 sn-p 以更好地了解它是什么:源代码(textuploader.com/57o57
    • 好吧,我疯了,为什么除了 html 没有找到任何元素,结果框架有不同的 url 链接,所以我一直在报错错误的源代码。我使用了你的解决方案,它奏效了。非常感谢!如果不是太问,你能帮我理解/preceding-sibling::td[1]/背后的逻辑吗?
    • Preceding-sibling 返回元素,位于同一级别的上下文元素之前(因此 siblings)。索引 1 表示只返回最近的前一个兄弟
    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2019-02-27
    • 2016-08-26
    • 2019-07-22
    • 2020-02-10
    相关资源
    最近更新 更多