如何定位id,class和xpath随每次刷新而变化的输入元素。 webdriver Python

时间:2017-03-30 09:17:07

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



<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>
&#13;
&#13;
&#13;

我正在尝试使用selenium Python webdriver登录.aspx网站,但问题是id classxpath正在通过重新加载或刷新进行更改。甚至,用户和密码输入也有多个input

source code screenshot:
UI screenshot:

1 个答案:

答案 0 :(得分:2)

您可以尝试使用以下XPath表达式:

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

表示Username字段和

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

代表Password