即使xpath正确,以下代码也会给我一个错误:
无法找到元素:{“method”:“xpath”,“selector”:“// * [@ id ='content column'] ...
我假设该字段中有多个xpath。这是代码。
driver.get("https://marswebtdc.tdc.vzwcorp.com/cdl/lte/fdr_llc/fdr.jsp?3gOr4g=4g");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
int i = 5;
String strI = Integer.toString(i);
WebElement element = driver.findElement(By.xpath("//*[@id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='content column']/table[1]/tbody/tr/td/form/b/table/tbody/tr/td/table/tbody/tr[2]/td[4]/input")).sendKeys(strI);
Thread.sleep(3000);
}
catch(Exception e){
System.out.println("Elemnt Not Found : "+e.getMessage());
}
Html代码段
<table align="left" width="100%" border="1" bgColor=#ffffff borderColorDark='white' borderColorLight='gray' cellpadding='2' cellspacing='0'><tr><td>
<table border='0' bgColor=#ffffff width="100%">
<tr>
<td colspan="4" align="left" nowrap>(Data available 07/30/2014 to 01/26/2015)</td>
<td rowspan="3" valign="top" align="center">
<input type="submit" name="action" value="Search"><br/>
<input type="reset" name="action" value=" Reset "><br/>
<input type="button" name="action" value=" Print " onClick="Print_onClick()"><br/>
<input type="button" name="action" value="Download CSV" onClick="parent.frames['results'].download_result()" disabled><br>
</td>
</tr>
<tr>
<td align="right" valign="top">Begin Date:</td>
<td valign="top">
<input type="text" name="txtBeginDate" value='01/26/2015' size="10" maxlength="10"
onBlur="doFormatDate(this)">
</td>
<td align="right" valign="top">MDN:</td>
<td valign="top"><input type="text" name="txtMsid" value="" size="15" maxlength="15" onKeyUp="removeStuff(this)" onBlur="removeStuff(this)" onPaste="return removeStuff(this)"></td>
形式form
java`
function validate(form)
{
var msg = "";
var bDate = form.txtBeginDate.value;
var eDate = form.txtEndDate.value;
var abDate = form.abd.value;
var aeDate = form.aed.value;
var msid = form.txtMsid.value;
if (!validDate(bDate))
msg += "o Begin Date is not a valid date.\n";
if (!validDate(eDate))
msg += "o End Date is not a valid date.\n";
if (compareDate(bDate, eDate))
msg += "o Begin Date must be less than or equal to End Date.\n";
if (compareDate(abDate, bDate) || compareDate(eDate, aeDate))
msg += "o Date entered are out of the available date range.\n";
if (calculateDays(bDate, eDate) > 30)
msg += "o Date range cannot be more than 30 days.\n";
if (isBlank(msid))
msg += "o MDN must be entered.\n";
if (!isNumber(msid) || (msid.length > 0 && msid.length < 10))
msg += "o MDN must be numeric and 10 digits in length.\n";
if (!isAllowedPhoneNumber(msid) ){
msg += "o MDN " + msid + " is not an allowed value.\n";
}
if (msg > "") {
msg = "Problems with data entry:\n" + msg;
alert(msg);
return false;
}
else {
if (!toggleSearch(false)) {
msg = "A search is pending! No new search may be issued until pending search returns.\n";
alert(msg);
return false;
}
else
return true;
}
}
</script>
<!--</head><body>-->
<td colspan="4" align="left" nowrap>(Data available 07/31/2014 to 01/27/2015)</td>
<td rowspan="3" valign="top" align="center">
<input type="submit" name="action" value="Search"><br/>
<input type="reset" name="action" value=" Reset "><br/>
<input type="button" name="action" value=" Print " onClick="Print_onClick()"><br/>
<input type="button" name="action" value="Download CSV" onClick="parent.frames['results'].download_result()" disabled><br>
</td>
</tr>
<tr>
<td align="right" valign="top">Begin Date:</td>
<td valign="top">
<input type="text" name="txtBeginDate" value='01/27/2015' size="10" maxlength="10"
onBlur="doFormatDate(this)">
</td>
<td align="right" valign="top">MDN:</td>
<td valign="top"><input type="text" name="txtMsid" value="" size="15" maxlength="15" onKeyUp="removeStuff(this)" onBlur="removeStuff(this)" onPaste="return removeStuff(this)"><enter code here/td>
答案 0 :(得分:1)
不要使用xpath,尤其是你的xpath。难以理解,阅读,维护等。阅读css selectors 。如果我正确理解你需要找到哪个元素,你可以尝试下一个选择器:
driver.findElement(By.Css("input#3gOr4g"));
或像这样smth =)
答案 1 :(得分:0)
如果您想找到“MDN:textfield”(如前面评论中所述),请尝试以下xpath:
1 - //input[@name='txtMsid']
这会将输入元素的name属性定位为'txtMsid'。
<强> 2 - 强> //td[contains(text(),'MDN')]/following-sibling::td[1]/input
这将在td元素下找到输入元素,这是'td'元素的第一个兄弟,它包含innerHTML / text作为'MDN'。