java - Selenium 不会通过 xpath 找到元素
问题描述
我正在使用 selenium webdriver 来自动更新 CRM 中的呼叫记录。在此过程中,我正在查找并选择要单击网页的元素。当我在 CRM 中搜索客户名称时出现问题,客户记录返回屏幕,但无论driver.findElement()
我使用什么,我都无法单击用户个人资料。有人可以让我知道为什么会这样吗?我尝试了以下几种方法:
driver.findElement(By.xpath("//*[@id=\"Leads_3461487000072987016\"]")).click();
或者
driver.findElement(By.xpath("/html/body/div[66]/div/div[2]/div/div[1]/div[1]/table[2]/tbody/tr/td/table/tbody/tr[2]/td[6]/a")).click();
我认为该元素可能位于不同的 iframe 上,但经过检查,我不相信它是。
基于检查的网页上的表格如下:
<table width="100%" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="top"> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="newtable topband_bb" id="gsearchTable"><tbody id="header_Leads"> <tr><th width="0" class="pL0 pR0"></th> <th nowrap=""> Company</th><th nowrap=""> Lead Source</th><th nowrap=""> Phone</th><th nowrap=""> Email</th><th nowrap=""> Full Name</th><th nowrap=""> Lead Owner</th></tr><script x-murphy-load-start="1607451890448">var jarr = [{"PHONE":"8502215904","SEID":"3461487000072987016","EMAIL":"chorton383@gmail.com","SMOWNERID":"<a id='gsearch_Salutation,First Name,Last Name' class=\"link\" data-cid='usersPermi_viewUser' purpose='showUserBC' data-params='{\"userId\":\"Salutation,First Name,Last Name\"}' >My User Name<\/a>","ISAPPROVED":1,"FULLNAME":"<a id='gsearch_3461487000072987016' href=\"/crm/org673229384/EntityInfo.do?id=3461487000072987016&module=Leads\" data-zcqa=\"gSearch_User name\" data-cid='detailView' data-params='{\"module\":\"Leads\",\"pfrom\":\"gsearch\",\"lookback\":true,\"id\":\"3461487000072987016\",\"recordNum\":\"1\"}' class=\"link\">User Name<\/a>"}];
var obtained_module = "Leads";
crmNavig.assignNavigCacheData("gsearch", obtained_module, 1, 10 ,jarr);//No I18N
var source = $('#tmpl_Leads').html();
var template_source = Handlebars.compile(source);
$("#header_Leads").append(template_source(jarr));
</script>
<tr class="topband_dv_row" id="Leads_3461487000072987016">
<td nowrap="" class="pR pL0 pR0"> <div id="icon_3461487000072987016" class="alignright pA w40 mainActionIcon" onclick="sE(event)"><link-to lt-prop-route="crm.tab.module.entity.edit" lt-prop-id="gsearchEdit_3461487000072987016" lt-prop-dp="["Leads","3461487000072987016"]" lt-prop-class="link neweditIcon dIB" data-zcqa="gSearchEditIcon" lt-prop-td="{"recordNum":"" }" lyte-rendered=""><a id="gsearchEdit_3461487000072987016" class="link neweditIcon dIB" href="/crm/org673229384/tab/Leads/3461487000072987016/edit"></a></link-to>
<a class="mL5 link deleteIcon dIB" data-params="{"module":"Leads"}" data-zcqa="gSearchDeleteIcon" href="javascript:;" onclick="showDeletePopup('gsearch_3461487000072987016', 'crm.button.move.to.trash', '', '', this, true);" data-onclick="javascript:searchDelAction("/crm/org673229384/MassDelete.do?module=Leads&fromIndex=1&toIndex=10&id=3461487000072987016&type=gsearch&sword=Username\x2BHorton&searchCategory=Leads&userOpt=null&isajax=true", "Leads")"></a>
</div>
</td><td></td> <td></td> <td>8502215904</td> <td>USER_EMAIL</td> <td><a id="gsearch_3461487000072987016" href="/crm/org673229384/EntityInfo.do?id=3461487000072987016&module=Leads" data-zcqa="gSearch_User NAME" data-cid="detailView" data-params="{"module":"Leads","pfrom":"gsearch","lookback":true,"id":"3461487000072987016","recordNum":"1"}" class="link">User Name</a></td> <td><a id="gsearch_Salutation,First Name,Last Name" class="link" data-cid="usersPermi_viewUser" purpose="showUserBC" data-params="{"userId":"Salutation,First Name,Last Name"}">My User Name</a></td> </tr></tbody><script x-murphy-load-start="1607451890448" id="tmpl_Leads" type="text/x-handlebars-template">
{{#each this}}
<tr class="topband_dv_row" id="Leads_{{SEID}}">{{#if ISAPPROVED}}
{{#ifCondLogic ISAPPROVED '!=' 2 '&&' ISAPPROVED '!=' 9}}
{{#ifCondLogic ISAPPROVED '=' '7' '||' ISAPPROVED '=' '8'}}
<td nowrap class="tableData bb" style="vertical-align:middle"> <div align="center" id="icon_{{SEID}}" style="visibility:hidden;"></div><span class="iconlock_ap mL2" title='{{getI18n "crm.reviewprocess.record.review.pending"}}' alt='{{getI18n "crm.reviewprocess.record.review.pending"}}'></span> {{else}}
<td nowrap class="pR pL0 pR0"> <div id="icon_{{SEID}}" class="alignright pA w40 mainActionIcon" onclick="sE(event)"><link-to lt-prop-route="crm.tab.module.entity.edit" lt-prop-id="gsearchEdit_{{SEID}}" lt-prop-dp='["Leads","{{SEID}}"]' lt-prop-class="link neweditIcon dIB" data-zcqa="gSearchEditIcon" lt-prop-td='{"recordNum":"{{recordNum}}" }'></link-to>
<a class="mL5 link deleteIcon dIB" data-params='{"module":"Leads"}' data-zcqa="gSearchDeleteIcon" href='javascript:;' onclick="showDeletePopup('gsearch_{{SEID}}', 'crm.button.move.to.trash', '', '', this, true);" data-onclick='javascript:searchDelAction("/crm/org673229384/MassDelete.do?module=Leads&fromIndex=1&toIndex=10&id={{SEID}}&type=gsearch&sword=User Name\x2BHorton&searchCategory=Leads&userOpt=null&isajax=true", "Leads")'></a>
</div>{{/ifCondLogic}}
{{else}}
<td nowrap class="tableData bb" style="vertical-align:middle"> <div align="center" id="icon_{{SEID}}" style="visibility:hidden;"></div>{{#ifEquals ISAPPROVED 9}}
<span class="iconlock_ap fR" title='Merging of duplicates is in progress.' alt='Merging of duplicates is in progress.'></span> {{else}}
<span class="iconlock_ap fR" title='Waiting for Approval' alt='Waiting for Approval'></span> {{/ifEquals}}
{{/ifCondLogic}}
{{else}}
<td nowrap class="pR pL0 pR0"> <div id="icon_{{SEID}}" class="alignright pA w40 mainActionIcon" onclick="sE(event)"><a class="link neweditIcon dIB" href="/crm/org673229384/EditEntity.do?module=Leads&id={{SEID}}" data-zcqa="gSearchEditIcon" id="gsearchEdit_{{SEID}}" data-cid="editbtn" data-params='{"module":"Leads","id":"{{SEID}}"}'></a><a class="mL5 link deleteIcon dIB" data-params='{"module":"Leads"}' data-zcqa="gSearchDeleteIcon" href='javascript:;' onclick="showDeletePopup('gsearch_{{SEID}}', 'crm.button.move.to.trash', '', '', this, true);" data-onclick='javascript:searchDelAction("/crm/org673229384/MassDelete.do?module=Leads&fromIndex=1&toIndex=10&id={{SEID}}&type=gsearch&sword=User Name\x2BHorton&searchCategory=Leads&userOpt=null&isajax=true", "Leads")'></a>
</div>{{/if}} </td><td>{{{COMPANY}}}</td> <td>{{{LEADSOURCE}}}</td> <td>{{{PHONE}}}</td> <td>{{{EMAIL}}}</td> <td>{{{FULLNAME}}}</td> <td>{{{SMOWNERID}}}</td> </tr>{{/each}}</script></table></td></tr></tbody></table>
解决方案
要click()
在元素上,您可以使用以下任一Locator Strategies:
使用cssSelector:
driver.findElement(By.cssSelector("table.newtable.topband_bb#gsearchTable tbody#header_Leads tr.topband_dv_row a[id^='gsearchEdit'][href*='Leads']")).click();
使用xpath:
driver.findElement(By.xpath("//table[@class='newtable topband_bb' and @id='gsearchTable']//tbody[@id='header_Leads']//tr[@class='topband_dv_row']//a[starts-with(@id, 'gsearchEdit') and contains(@href, 'Leads')]")).click();
但是,由于元素是动态元素,因此click()
在元素上您需要诱导WebDriverWait并且elementToBeClickable()
您可以使用以下任一Locator Strategies:
使用cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.newtable.topband_bb#gsearchTable tbody#header_Leads tr.topband_dv_row a[id^='gsearchEdit'][href*='Leads']"))).click();
使用xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='newtable topband_bb' and @id='gsearchTable']//tbody[@id='header_Leads']//tr[@class='topband_dv_row']//a[starts-with(@id, 'gsearchEdit') and contains(@href, 'Leads')]"))).click();
推荐阅读
- c# - .NET - 将 DI 注入的对象替换为单例
- javascript - 如何在使用 wkhtmltopdf 将 html 转换为 pdf 之前确定页面的结尾?
- python - anaconda Jupyter 笔记本中的内核错误
- python-3.x - Pythonic 减少子类的方法
- mysql - 如何使用 MySQL 进行最相关的搜索?
- python - Python Pillow/PIL 变色问题。在 GIF 中
- xml - 使用 XPath 选择除具有与父级相同标记的子级之外的所有子级
- javascript - 如何以角度在http POST中发送图像和更多键值
- terraform - terraform 中的 appAutoScaling 属性与自动缩放属性有什么区别?
- python - Python ttk组合框值更新?