【问题标题】:How to target input element whose id, class and xpath changing with every refresh. webdriver Python如何定位其 id、class 和 xpath 随每次刷新而变化的输入元素。网络驱动程序 Python
【发布时间】:2017-03-30 09:17:07
【问题描述】:

<div class="label-txt">User Name</div> 
    <input name="xugw1irortn1ogkpy31wxl" type="text" id="xugw1irortn1ogkpy31wxl" style="display:none;" />
    <input name="bzzunemwbpxl44jnmxr0bt" type="text" id="bzzunemwbpxl44jnmxr0bt" style="display:none;" />
    <input name="w4gxjqkwvd4pqp4xrevu2r" type="text" id="w4gxjqkwvd4pqp4xrevu2r" style="display:none;" />
    <input name="bzxdws3vj041exduu1125j" type="text" id="bzxdws3vj041exduu1125j" style="display:none;" />
    <input name="lun5ji01ou4wpyvzaypji1" type="text" id="lun5ji01ou4wpyvzaypji1" style="display:none;" />
    <input name="x1rkfxp4sjwojbvr2uptuz" type="text" id="x1rkfxp4sjwojbvr2uptuz" style="display:none;" />
    <input name="cptv2l0ffvzpwqk0wetlnh" type="text" id="cptv2l0ffvzpwqk0wetlnh" style="display:none;" />
    <input name="couyswy1yfm51bpnyzkyrg" type="text" id="couyswy1yfm51bpnyzkyrg" style="display:none;" />
    <input name="yninikw54iudxqqlzgmxir" type="text" id="yninikw54iudxqqlzgmxir" style="display:none;" />
    <input name="ngb4uczri1l5ftyqvppbgk" type="text" id="ngb4uczri1l5ftyqvppbgk" style="display:none;" />
    <input name="e5dt2tozqrbrqxsivl0ygk" type="text" id="e5dt2tozqrbrqxsivl0ygk" style="display:none;" />
    <input name="rkcjmknvghdly0vk03ynmf" type="text" id="rkcjmknvghdly0vk03ynmf" style="display:none;" />
    <input name="moostndi5vtvpc0fy5sboa" type="text" id="moostndi5vtvpc0fy5sboa" style="display:none;" />
    <input name="1bps10gceliasg41iy455l" type="text" id="1bps10gceliasg41iy455l" style="display:none;" />
    <input name="a1z1amtyvnrtad55ywiqnc" type="text" id="a1z1amtyvnrtad55ywiqnc" style="display:none;" />
    <input name="m3jyv1ibdslgoelr5wdlfd" type="text" id="m3jyv1ibdslgoelr5wdlfd" style="display:none;" />
    <input name="4dgqbpwew3ngbgwdnxaqf4" type="text" id="4dgqbpwew3ngbgwdnxaqf4" style="display:none;" />
    <input name="hocvr1ykprqs5a4agnnfie" type="text" id="hocvr1ykprqs5a4agnnfie" />
    <span id="ctl03" style="color:Red;visibility:hidden;">*</span>
</div>

<div class="label-txt">Password</div> 

 <input name="xugw1irortnb5sraht3azj" type="password" id="xugw1irortnb5sraht3azj" style="display:none;" />
 <input name="bzzunemwbpxb1vpydi1qcv" type="password" id="bzzunemwbpxb1vpydi1qcv" style="display:none;" />
 <input name="w4gxjqkwvd4n50vqqehcab" type="password" id="w4gxjqkwvd4n50vqqehcab" style="display:none;" />
 <input name="bzxdws3vj04w2iargab32a" type="password" id="bzxdws3vj04w2iargab32a" style="display:none;" />
 <input name="lun5ji01ou42tafgvmhc4t" type="password" id="lun5ji01ou42tafgvmhc4t" style="display:none;" />
 <input name="x1rkfxp4sjwtlwvdpsdjkq" type="password" id="x1rkfxp4sjwtlwvdpsdjkq" style="display:none;" />
 <input name="cptv2l0ffvznmop4iqycoe" type="password" id="cptv2l0ffvznmop4iqycoe" style="display:none;" />
 <input name="couyswy1yfm22nbvshuvrv" type="password" id="couyswy1yfm22nbvshuvrv" style="display:none;" />
 <input name="yninikw54iub5zucjoseb2" type="password" id="yninikw54iub5zucjoseb2" style="display:none;" />
 <input name="ngb4uczri1lmusbh0ztgkf" type="password" id="ngb4uczri1lmusbh0ztgkf" style="display:none;" />
 <input name="e5dt2tozqrbiseaipzetp1" type="password" id="e5dt2tozqrbiseaipzetp1" style="display:none;" />
 <input name="rkcjmknvghdfdmhcpfhvrv" type="password" id="rkcjmknvghdfdmhcpfhvrv" style="display:none;" />
 <input name="moostndi5vtep5rwqi5llx" type="password" id="moostndi5vtep5rwqi5llx" style="display:none;" />
 <input name="1bps10gceliy02zzdkpx5p" type="password" id="1bps10gceliy02zzdkpx5p" style="display:none;" />
 <input name="a1z1amtyvnrdygk03jr3vf" type="password" id="a1z1amtyvnrdygk03jr3vf" style="display:none;" />
 <input name="m3jyv1ibdslxju0pbgrmbn" type="password" id="m3jyv1ibdslxju0pbgrmbn" style="display:none;" />
 <input name="4dgqbpwew3n5ru3dbxnzxo" type="password" id="4dgqbpwew3n5ru3dbxnzxo" style="display:none;" />
 <input name="hocvr1ykprqlzaevxie2iv" type="password" id="hocvr1ykprqlzaevxie2iv" />
 <span id="ctl04" style="color:Red;visibility:hidden;">*</span>
</div>

我正在尝试使用 selenium Python webdriver 登录 .aspx 网站,但问题是 id classxpath 正在随着重新加载或刷新而改变。甚至,还有多个input供用户和密码输入。

source code screenshot:
UI screenshot:

【问题讨论】:

  • @JainishKapadia , HTML 代码添加
  • @TheBlueMagician,分享你已经尝试过的代码(选择器)

标签: python selenium xpath selenium-webdriver selenium-chromedriver


【解决方案1】:

你可以尝试使用下面的XPath表达式:

//div[text()="User Name"]/following-sibling::input[not(@style="display:none;")]

对于Username 字段和

//div[text()="Password"]/following-sibling::input[not(@style="display:none;")]

Password

【讨论】:

  • @Anderson your xpath for user name 找到这两个元素。
  • 是的。它应该返回 2 个元素。但是使用find_element_by_xpath(XPATH) 会找到第一个 - 所需的。 OP 可以使用find_elements_by_xpath(XPATH)[0],但我认为这些没有理由
  • 所以,我想我们应该在xpath 结束后使用indexing,就像这样//div[text()='User Name']/following-sibling::input[not(@style="display:none;")][1]
  • 没有。如果我们需要使用Passwordinput字段和相同的XPath,那么使用索引是可以的,但是在这种情况下不需要因为需要的元素已经是第一个找到的
猜你喜欢
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 1970-01-01
相关资源
最近更新 更多