我尝试使用以下HTML标记点击页面上的按钮:
<html lang="en" webdriver="true">
<head>
<body class="scbody" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAKCAYAAAB10jRKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBJREFUeNpEyqERADAIBMGbbwSH+qhA/6URXMyqBUhFBLIP6ip0ezk2zExqC58nwACVZwX67tO41gAAAABJRU5ErkJggg=="), url("http://ir.ebaystatic.com/rs/v/res.img.pics.cmp.ds3.imgbg_jpg"); background-repeat: repeat-x, repeat; background-position: 0px 30px, 0px 0px;">
<div id="gh-gb" tabindex="-1"/>
<div class="scCont clearfix">
<div class="fl ml10 mr10">
<div id="globalheader" class="fl col_100p clearfix">
<div id="PageTitle" class="fl col_100p clearfix">
<div id="centralarea" class="fl col_100p clearfix">
<div id="rightSection" class="fr clearfix">
<div id="leftsection" class="fl clearfix">
<div id="CARTSection" class="fl dib col_100p">
<div id="ShopCart" class="fl col_100p">
<div class="c-std">
<p/>
<div id="sellerBucket_adoramacamera" class="fl col_100p">
<div id="sellerBucket_usacoinsupplies" class="fl col_100p">
<div id="sc_usacoinsupplies" class="fl cart_sci b-ddd bgclr-fcfcfc col_100p">
<div id="usacoinsupplies" class="sel_row sel_info_row" data-rtdisable="true" data-memberid="usacoinsupplies" data-count="2">
<div class="fl lalign ralign mw851px">
<div id="usacoinsupplies-itemGroup1" class="fl col_100p clearfix">
<div class="fl col_100p ">
<div id="usacoinsupplies-itemGroup1-item1" class="fl col_100p clearfix">
<div id="8460053414" class=" fl col_100p talign balign clearfix" data-varid="0" data-itemid="141494853656" data-hasmsg="false" data-displayorder="2" data-qty="1" data-iinfo="eyJpdGVtSWQiOiIxNDE0OTQ4NTM2NTYiLCJpdGVtUmVmZXJlbmNlSWQiOiI4NDYwMDUzNDE0IiwiaXRlbVBvc2l0aW9uIjpudWxsLCJpdGVtTm90aWZpY2F0aW9uQWN0aW9uIjpudWxsLCJpdGVtTm90aWZpY2F0aW9uVHlwZSI6bnVsbCwiaXRlbU5vdGlmaWNhdGlvblRyYWNraW5nUHJvcGVydHkiOm51bGwsIml0ZW1RdWFudGl0eSI6MSwiaXRlbVZhcmlhdGlvbklkIjoiMCIsIml0ZW1TaGlwcGluZ1NlcnZpY2VJZCI6IjIiLCJpdGVtVGl0bGUiOiI1eCBBcHBsZSBpUGhvbmUgNS81cy81YyBTY3JlZW4gUHJvdGVjdG9yIiwiaXRlbVR5cGUiOiJGSVhFRF9QUklDRSIsImJvcGlzIjpmYWxzZSwiYm9waXNTdG9yZVNlbGVjdGVkIjpmYWxzZSwic2hpcHBpbmdTZXJ2aWNlTWFwIjp7IjIiOiJFeHBlZGl0ZWQgU2hpcHBpbmdfRXhwZWRpdGVkU2hpcHBpbmciLCIxOCI6Ik9uZS1kYXkgU2hpcHBpbmdfT25lRGF5U2hpcHBpbmcifSwic3RvcmVJZCI6bnVsbCwibG9jYXRpb25JZCI6bnVsbCwicGlja3VwTWV0aG9kVG9rZW4iOm51bGwsInBpY2t1cE1ldGhvZElkZW50aWZpZXIiOm51bGwsImVibiI6ZmFsc2UsImViblNlbGVjdGVkIjpmYWxzZX0*">
<div class="fr col_100p clearfix">
<div class="fr col_100p clearfix">
<div class="fr col_100p prltv">
<div class="tr">
<a class="action actionLink" aria-describedby="141494853656_title" aria-label="Remove" href="http://cart.payments.ebay.com/sc/rfc?iref=8460053414" data-irefid="8460053414">Remove</a>
<span class="mr10 ml10 linkClr">|</span>
<a class="action actionLink" aria-describedby="141494853656_title" aria-label="Save for later" href="http://cart.payments.ebay.com/sc/sfl?iref=8460053414" data-irefid="8460053414">Save for later</a>
</div>
</div>
</div>
<div class="ie7mb15"/>
代码的主要部分是:
<a class="action actionLink" aria-describedby="141494853656_title" aria-label="Remove" href="http://cart.payments.ebay.com/sc/rfc?iref=8460053414" data-irefid="8460053414">Remove</a>
<span class="mr10 ml10 linkClr">|</span>
<a class="action actionLink" aria-describedby="141494853656_title" aria-label="Save for later" href="http://cart.payments.ebay.com/sc/sfl?iref=8460053414" data-irefid="8460053414">Save for later</a>
我想过使用aria-describeby
属性动态保存项目,如下所示:
".//*[@aria-describedby=" + "'" + itemID + "_title']/div[2]/div/div/a[2]"
然而,由于某种原因,它永远不会奏效。我做错了什么?
注意:可以有多个项目(我可以想到)识别特定项目的唯一方法是itemID - 因为其他属性是动态生成的。
由于
答案 0 :(得分:0)
我认为使用xpath是不好的做法,尝试点击使用cssSelector:
driver.findElement(By.cssSelector(".action")).click();
如果你想逐个属性地使用这样的东西,例如:
By.CssSelector("button[name='hasan']");