亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從Selenium提供的html中提取動態文本3204255?

如何從Selenium提供的html中提取動態文本3204255?

慕標琳琳 2021-08-19 18:46:51
我正在嘗試從 div 中獲取數字。當我執行我的測試用例時,新數字正在顯示,現在我想獲取該數字,或者換句話說,將其打印在控制臺中。我正在使用:webElement webelement=driver.findElement(By.xpath("//div[contains(@class,'responsive now') and text()='Application number']"));webelement.getText();和driver.findElement(By.xpath("//div[@class='responsive-show']")).getAttribute("value");它不是關于定位器的compailining,但它沒有顯示數字。<td>   <div class="responsive-show">Application number</div>  "3204255"   <input data val="true" data-val-number="The field ExaminationNumber must be a number." data-val-required="The ExaminationNumber field is required." id="ActiveExamModeIs_0__ExaminationNumber" name="ActiveExamMode[0].ExaminationNumber" type="hidden" value="3204255">   <input data -val="true" data-val-number="The field ExaminationEligibilityExamTypeId must be a number" data-val-required="The ExaminationEligibilityExamTypeId fiels is required." type="hidden" value="1"></td>
查看完整描述

3 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

長短不一:除非您想使用“JavascriptExecutor”,否則沒有一種使用簡單的 Selenium 函數獲取文本值的簡潔方法


另一種選擇是<td>使用簡單的硒操作從元素中獲取整個文本,然后使用 String 函數substring()獲取您的應用程序編號值,該值卡在引號之間"。


代碼:


    String input = driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td")).getAttribute("innerText");

    input = input.substring(input.indexOf("\"")+1, input.lastIndexOf("\""));

    System.out.println("Extracted Value" + input);


查看完整回答
反對 回復 2021-08-19
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

根據您共享的HTML提取數字,即3204255,您需要誘導WebDriverWait以使所需元素可見,然后您可以executeScript()按照以下解決方案使用該方法:


Java解決方案:


WebElement myElement = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[@class='table table-hover']//tbody/tr/td")));

String myText = ((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[2].textContent;', myElement).toString();



查看完整回答
反對 回復 2021-08-19
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

text: 3204247 不包含在 div 標簽中,而是包含在<td>. 你可以試試這個:


webElement webelement=driver.findElement(By.xpath("//table[@class='table table-hover']/tbody/tr/td"));// you can improve this locator and can also use one below

//webElement webelement=driver.findElement(By.xpath("//div[@class='responsive-show']/..")); using xpath axes

String value=webelement.getText();// OR

//String value=webelement.getAttribute("innerText");


查看完整回答
反對 回復 2021-08-19
  • 3 回答
  • 0 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號