遇到一个特殊的问题,我无法使用Selenium将文本输入到TextBox中。
到目前为止,我已经尝试了以下方法,但无法与其中任何一个取得结果:
SendKeys
动作类
javascriptexecutor
有关受测网页部分的详细信息:
页面部分有两个单选按钮,使用户可以选择遵循不同的路径进行验证。
即使用户选择我认为可能是问题的两个复选框中的任何一个,我面临问题的字段的Xpath也保持不变。
这不是与帧切换有关的问题,因为本节中的其他字段工作正常。
结果:
代码可以单击该字段,但不能输入文本。
即使通过javascriptexecutor在5次输入文本中有2次,页面也无法识别该文本。由于它是必填字段,因此即使通过javascript填充了文本,也会在以后引发错误。
期望:
需要一种可靠的方法来使其工作。任何方法都行。
方法1:SendKeys
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='cPATCHEDOmniScriptCommunity']")));
driver.findElement(By.xpath("//div[@ng-show='creditCheckDetails && !successCreditBoolVal']/div[2]/div[1]/input[1]")).sendKeys("TestData");
方法2:操作类
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='cPATCHEDOmniScriptCommunity']")));
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//div[@ng-show='creditCheckDetails && !successCreditBoolVal']/div[2]/div[1]/input[1]"))).sendKeys("TestData").build().perform();
方法3:Javascript执行程序
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='cPATCHEDOmniScriptCommunity']")));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='testdata';", driver.findElement(By.xpath("//div[@ng-show='creditCheckDetails && !successCreditBoolVal']/div[2]/div[1]/input[1]")));
方法4:Javascript执行程序
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='cPATCHEDOmniScriptCommunity']")));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.evaluate(//div[@ng-show='creditCheckDetails && !successCreditBoolVal']/div[2]/div[1]/input[1], document, null, 9, null).singleNodeValue.innerHTML="+ BusinessName);
页面HTML:
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center tileContainer b-t-4" ng-class="{'b-t-4': creditCheckDetails, 'b-t-1': !creditCheckDetails}" ng-show="showCreditSectionForAgent" style="">
<h2 class="titleHeader ng-binding">Business verification</h2>
<div class="arrow_padding isMobile"><img src="/resource/vz_images/images/arrow.svg" class="tileIcon isMobile ng-hide" ng-click="completePlaceOrder = false; creditCheckDetailsMobile = true;scrollToTopOfPage();" ng-show="!creditCheckDetails && !successCreditBoolVal && showPlus" style=""></div>
<img src="/resource/BuyflowImages/BuyflowImages/plus-large.svg" class="tileIcon isNotMobile ng-hide" ng-click="insatllationDetails = false; contactDetails = false; shippingAddressDetails = false; billingAddressDetails = false; termsAndConditionDetails = false; cardMessageCheck(); creditCardDetails = false; creditCheckDetails = !creditCheckDetails" ng-show="!creditCheckDetails && !successCreditBoolVal && showPlus" style="">
<img src="/resource/BuyflowImages/BuyflowImages/minus-large.svg" class="tileIcon isNotMobile" ng-click="insatllationDetails = false; contactDetails = false; shippingAddressDetails = false; billingAddressDetails = false; termsAndConditionDetails = false; cardMessageCheck(); creditCheckDetails = !creditCheckDetails" ng-show="creditCheckDetails && !successCreditBoolVal" style="">
<div class="slds-size--1-of-1 ng-hide" ng-show="!creditCheckDetails && !showHelpMessage" style="">
<p class="Body warn-state" ng-show="!successCreditBoolVal">Please complete credit check.</p>
<p class="Body warn-state ng-binding ng-hide" ng-show="creditCheckError"></p>
<p class="Body success-state ng-hide" ng-show="successCreditBoolVal">Credit approval complete</p>
</div>
<div class="slds-size--1-of-1 vz_alert-warn helpMessage ng-binding ng-hide" ng-show="showHelpMessage && !successCreditBoolVal" ng-bind-html="helpMessage"></div>
<div class="slds-size--1-of-1" ng-show="creditCheckDetails && !successCreditBoolVal" style="">
<p class="Body">As part of your new service request, we need to do a standard business verification using your Federal Tax ID. If you do not have a Tax ID, we can use your personal information as well.</p>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center creditCheckSolePCorpLLC" style="padding-top: 2%;">
<div class="slds-size--1-of-1">
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--8-of-12 slds-grid_vertical-align-center slds-grid_align-center">
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="padding-right: 10px;">
<label class="container-radio">
<input type="radio" name="soleOrCorp" value="corporationLLC" ng-model="soleOrCorp" style="-webkit-appearance: radio;" class="ng-pristine ng-untouched ng-valid ng-not-empty">
<span class="checkmark"></span>
<span for="contactChoice2" style="font-family: 'NeueHaasGroteskText'; font-size: 14px; color: black;">My Business uses a Federal Tax ID</span>
</label>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center">
<label class="container-radio">
<input type="radio" name="soleOrCorp" value="soleproprietorship" ng-model="soleOrCorp" style="-webkit-appearance: radio;" class="ng-pristine ng-untouched ng-valid ng-not-empty">
<span class="checkmark"></span>
<span style="font-family: 'NeueHaasGroteskText'; font-size: 14px; color: black;">My Business does not have a Federal Tax ID</span>
</label>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-large-size--6-of-12"></div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center corpLLCSoleContainer" style="padding-top:1%;" ng-show="soleOrCorp === 'corporationLLC'">
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="display: block;padding-right: 30px;">
<label class="labelAboveTextBox">Business name*</label>
<input type="text" class="businessName ng-pristine ng-valid ng-empty ng-valid-maxlength ng-touched" ng-model="businessName" ng-keyup="setBusinessName(businessName)" maxlength="30" style="width: 100%;">
<!-- ngIf: reqForBusinessName -->
<!-- ngIf: reqForBusinessNamesalphabets -->
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="display: block;padding-right: 30px;">
<label class="labelAboveTextBox">Federal Tax ID*</label>
<input type="password" ng-required="businessTaxId" class="businessTaxId ng-pristine ng-empty ng-valid ng-valid-required ng-touched" ng-model="businessTaxId" ng-change="Check(businessTaxId, 'tId', 'businessTaxId')" ng-blur="PIEEncryptionTax()" ng-class="{'vz_error': businessTaxId.length > 0 && businessTaxId.length !== 9, 'noredBorder': businessTaxId.length === 9}" style="width: 100%;">
<!-- ngIf: reqForBusinessTaxIDForLength -->
<!-- ngIf: reqForBusinessTaxIDFornozero -->
<!-- ngIf: reqForBusinessTaxID -->
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="display: block;padding-right: 30px;margin-top: 15px;">
<label class="labelAboveTextBox">Responsible Party First Name*</label>
<input type="text" class="businessName afname ng-pristine ng-untouched ng-valid ng-not-empty" id="responsibeFirstName" ng-keyup="responsibeFirstName();" ng-model="afname" style="width: 100%;">
<!-- ngIf: reqafname -->
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="display: block;margin-top: 15px;padding-right: 30px;">
<label class="labelAboveTextBox">Responsible Party Last Name*</label>
<input type="text" class="businessName alname ng-pristine ng-untouched ng-valid ng-not-empty" id="responsibeLastName" ng-keyup="responsibeLastName();" ng-model="alname" style="width: 100%;">
<!-- ngIf: reqalname -->
</div>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center corpLLCSoleContainer ng-hide" style="" ng-show="soleOrCorp === 'soleproprietorship' ">
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center">
<p class="Body ng-binding"></p>
<div ng-hide="hideForAgent" class="slds-m-top--medium" style="width: 100%;">
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center soleProprietorInputDiv" style="padding-bottom: 5px;padding-top:5px;">
<div class="slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12" style="padding-right: 30px;">
<label class="labelAboveTextBox">Business name*</label>
<input type="text" class="sBusinessName ng-pristine ng-untouched ng-empty ng-valid-maxlength ng-valid ng-valid-required" ng-model="businessName" ng-required="businessName" ng-keyup="setBusinessName(businessName)" maxlength="30" style="width: 100%;>
<span ng-if=" reqsbusinessname"="">This is required
</div>
<div class="slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--6-of-12" style="padding-right: 30px;">
<label class="labelAboveTextBox">SSN*</label>
<input type="password" class="ssn ng-pristine ng-untouched ng-empty ng-valid ng-valid-required" ng-required="ssnId" ng-model="ssnId" ng-blur="PIEEncryptionSsn()" ng-change="Check(ssnId, 'ssn', 'ssn')" ng-class="{'vz_error': ssnId.length > 0 && ssnId.length !== 9, 'noredBorder': ssnId.length === 9}" style="width: 100%;>
<span ng-if=" reqssnidlen"="">SSN Length should be 9
<!-- ngIf: reqssnIdlpatternDups -->
<!-- ngIf: reqssnIdlpattern -->
<!-- ngIf: reqssnId --> </div>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="padding-top: 25px; padding-bottom: 10px;">
<div class="slds-size--1-of-1" style="font-size: 14px; font-weight: bold; font-family: 'NeueHaasGroteskDisplayBold'; color: black;">
Please provide the name of person responsible for the account
</div>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center ssnHolderNameDiv" style="padding-bottom: 5px;">
<div class="slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--3-of-12" style="padding-right: 30px;">
<label class="labelAboveTextBox">Responsible Party First Name*</label>
<input type="text" class="fname ng-pristine ng-untouched ng-valid ng-not-empty" ng-model="fname" style="width: 100%;" reqfname"="">This is required
</div>
<div class="slds-size--1-of-1 slds-medium-size--3-of-6 slds-large-size--3-of-12" style="padding-right: 30px;">
<label class="labelAboveTextBox">Responsible Party Last Name*</label>
<input type="text" class="lname ng-pristine ng-untouched ng-valid ng-not-empty" ng-model="lname" style="width: 100%;" reqlname"="">This is required
</div>
<div class="slds-large-size--6-of-12"></div>
</div>
<div class="slds-grid slds-wrap slds-gr id_vertical-align-center slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center" style="margin-bottom: 0px;padding-top: 15px;">
<label class=" labelAboveTextBox">Date of Birth*</label>
</div>
<div class="slds-grid slds-wrap slds-grid_vertical-align-center slds-medium-size--6-of-6 slds-large-size--12-of-12 slds-grid_vertical-align-center slds-grid_align-center" id="ssnDOBdiv" style="padding-bottom: 5px;margin-top: 0.3%;">
<div class="slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--4-of-12">
<label class="labelAboveTextBox">MM/DD/YYYY</label>
<input type="text" datepicker-credit-section="" class="yYYY ng-pristine ng-untouched ng-valid ng-empty" ng-model="ssnDOB" readonly="" id="creditCheckDOBY" ng-blur="PIEEncryptionDOB()" style="width: 100%;>
</div>
<div class=" slds-large-size--6-of-12"=""></div>
</div>
<div><!-- ngIf: reqDOB --></div>
</div>
</div>
</div>
<div class="slds-size--1-of-1 slds-medium-size--6-of-6 slds-large-size--12-of-12">
<button class="btn-buyF btn-primary-buyF btn-md-buyF save-btn ng-binding" ng-class="{'confirm-btn-disabled': (successCredit || hideForAgent)}" ng-click="scrollToTop(); validateCreditCheckData();" style="height: 42px;border-radius: 21px;">Proceed</button>
</div>
</div>
</div>
答案 0 :(得分:0)
尝试以下Xpath。
//input[@class='businessName ng-pristine ng-valid ng-empty ng-valid-maxlength ng-touched'][@ng-model='businessName']
*编辑后的答案**
//label[@class='labelAboveTextBox'][contains(.,'Business name')]/following-sibling::input
已更新
使用WebdriverWait和elementtobeclickable并使用以下xpath。由于存在两个类似的元素,并且您说过每次自动化运行中类名都在更改,因此我为第一个元素提供了索引选项。
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//label[@class='labelAboveTextBox'][contains(.,'Business name')]/following-sibling::input)[1]")));
element.sendKeys("TestData");