Selenium WebDriver无法单击两个继续SPAN按钮

时间:2019-05-20 16:54:55

标签: selenium webdriver

我试图用以下HTML代码单击两个按钮

1。

 <span translate="Resources.AddOns.TravelInsurance.Button_ContinueBooking" class="ng-scope">Continue Booking</span>
 The Xpath = //*[@id="cont_to_ins"]/span
  1. <button wn-validation-dom="prepaidcards-details" class="orange-btn btn btn-sm bold ng-scope" ng-if="!areOtherAddOnsAfterPrepaidCardsIndexAvailableInAccordion()" id="cont_to_ins" ng-click="continueBooking()"><span translate="Resources.AddOns.PrepaidCards.Button_ContinueBooking" class="ng-scope">Continue Booking</span> <i class="material-icons "></i></button>

Xpath =

//*[@id="cont_to_ins"]

我尝试了几种代码,但收到错误消息,即无法单击按钮。

请协助查找以下元素的代码,然后单击以移至下一页

driver.findElement(By.xpath("//span[contains(text(), 'Continue Booking')])[1]/following::button[1]")).click();
   log.debug("Click Continue ");




WebElement elementa=  driver.findElement(By.xpath("//span[text()='Continue Booking']/"));   
    JavascriptExecutor executore = (JavascriptExecutor) driver;
    executore.executeScript("arguments[0].click();", elementa);
  

org.openqa.selenium.InvalidSelectorException:无效的选择器:无法   用xpath表达式定位元素// span [contains(text(),   'Continue Booking')])[1] / following :: button [1]因为以下原因   错误:语法错误:无法对“文档”执行“评估”:   字符串'// span [contains(text(),'Continue   Booking')])[1] / following :: button [1]'不是有效的XPath表达式。   (会话信息:chrome = 74.0.3729.157)(驱动程序信息:   chromedriver = 74.0.3729.6   (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs / branch-heads / 3729 @ {#29}),平台= Windows   NT 10.0.17763 x86_64)(警告:服务器未提供任何   stacktrace信息)命令持续时间或超时:0毫秒   有关此错误的文档,请访问:   https://www.seleniumhq.org/exceptions/invalid_selector_exception.html   构建信息:版本:“未知”,修订版:“未知”,时间:“未知”   系统信息:主机:'WN-HQ-LT-DEV-42',ip:'10 .10.11.250',操作系统名称:   'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:   '1.8.0_211'驱动程序信息:org.openqa.selenium.chrome.ChromeDriver   功能{acceptInsecureCerts:false,acceptSslCerts:false,   applicationCacheEnabled:否,browserConnectionEnabled:否,   browserName:chrome,chrome:{chromedriverVersion:74.0.3729.6   (255758eccf3d24 ...,userDataDir:C:\ Users \ ADEYIN〜1.ALA \ AppDa ...},   cssSelectorsEnabled:true,databaseEnabled:false,goog:chromeOptions:   {debuggerAddress:本地主机:64341},handlesAlerts:true,   hasTouchScreen:false,javascriptEnabled:true,   locationContextEnabled:true,mobileEmulationEnabled:false,   nativeEvents:true,networkConnectionEnabled:false,pageLoadStrategy:   正常,平台:XP,平台名称:XP,代理:Proxy(),可旋转:   false,setWindowRect:true,strictFileInteractability:false,   takesHeapSnapshot:true,takesScreenshot:true,超时:{隐式:   0,pageLoad:300000,脚本:30000},unantAlertBehaviour:忽略,   unhandledPromptBehavior:忽略,版本:74.0.3729.157,   webStorageEnabled:true}会话ID:aebe53b2f0ce483db0acf2034848ecd9   ***元素信息:{使用= xpath,值= // span [包含(text(),'继续预订')]))[1] /以下:: button [1]}

2 个答案:

答案 0 :(得分:0)

使用WebdriverWait处理动态元素并跟随Xpath

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//button[@id="cont_to_ins"]/span[contains(.,"Continue Booking")]')));
element.click()

答案 1 :(得分:0)

您尚未编写正确的XPath

driver.findElement(By.xpath(“ ((// span [contains(text(),'Continue Booking')]])[1] / following :: button [1]”)) .click();    log.debug(“点击继续”);