gpt4 book ai didi

Java + Selenium : Unable to Locate Element by Attribute

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:37 24 4
gpt4 key购买 nike

我正在尝试单击具有以下 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 - 因为其他属性是动态生成的。

谢谢

最佳答案

我认为使用 xpath 是不好的做法,尝试使用 cssSelector 单击:

driver.findElement(By.cssSelector(".action")).click();

如果您想按属性获取元素,请使用类似这样的内容:

By.CssSelector("button[name='hasan']");

关于Java + Selenium : Unable to Locate Element by Attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com