gpt4 book ai didi

java - 如何使用 Angular 方法将数据检索到 selenium java 脚本中

转载 作者:行者123 更新时间:2023-12-01 09:30:38 28 4
gpt4 key购买 nike

我想从“visitor”或“visitor.VisitorId”检索访客 ID。但下面的代码我用来检索数据,但成功运行,没有任何错误,但我收到的值为空。

HTML 代码:-

<ul class="sidebar-menu">
<li id="visitorView" class="treeview active">
<a>
<ul id="visitorViewMenu" class="treeview-menu menu-open" style="display: block;">
<!-- ngRepeat: visitor in Visitors -->
<li class="ng-scope" ng-repeat="visitor in Visitors" style="">
<a id="visitor.VisitorId" class="ng-binding" ng-click="select(visitor)">
<countryflag class="flagimg ng-isolate-scope" visitor="visitor">
<span class="chattabname"/>
A
<span class="timmer1 pull-right" runtimer="{"VisitorID":"c2c45b4d-5077-492f-afd6-88ab3bba99cd","Name":"A","StartTime":"2016-09-09 10:33:21","WidgetId":"7fcf22c6-4a9d-4701-9865-b8a85d597862","ConnectionId":"edc7d72b-8217-4961-81ff-f4ef4138bc3b","TimeZone":"Asia/Colombo","CountryCode":"lk","VisitorName":null,"Department":null,"CompanyId":"a4afbd8b-1de9-49d9-8fe6-4ec8119f4bb8"}">
</a>
</li>
<!-- end ngRepeat: visitor in Visitors -->
<li>
</ul>
</li>
<li class="treeview">
<li class="treeview">
</ul>

Selenium 代码:-

**1st method :-** 
WebElement cityField = driver.findElement(By.cssSelector("a[ng-click='select(visitor)']"));

**2nd method :-**
WebElement cityField = driver.findElement(By.cssSelector("a[id='visitor.VisitorId']"));

**Output**
System.out.println("+++-- "+cityField.getAttribute("value"));

最佳答案

尝试使用 getText() 这将返回 innerText <a>的元素如下:-

WebElement cityField = driver.findElement(By.id("visitor.VisitorId"));
System.out.println("+++-- " + cityField.getText());

或者如果你想获得 span元素其中 visitorId存在于 runtimer属性值,您应该找到 span元素并获取runtimer属性值为:-

WebElement cityField = driver.findElement(By.cssSelector("a[id = 'visitor.VisitorId'] span.timmer1"));
String runtimeData = cityField.getAttribute("runtimer");

//Now do some programming stuff to retrieve visitor id

runtimer属性数据类似于 json格式,因此您可以在转换为 org.json.JSONObject 后检索任何数据通过传递他们的 key如下:-

import org.json.JSONException;
import org.json.JSONObject;

public static Object getValue(String data, String key) throws JSONException {
JSONObject jObject = new JSONObject(data);
return jObject.get(key);
}


String visitorID = (String) getValue(runtimeData, "VisitorID");
System.out.println(visitorID);

输出:-

c2c45b4d-5077-492f-afd6-88ab3bba99cd

正如OP建议的,我们可以使用split()函数也可以检索数据:-

String[] splitS = runtimeData.split(","); 

for(int i =0; i < splitS.length; i++)
{
System.out.println("splitS" + splitS[i]);
}

关于java - 如何使用 Angular 方法将数据检索到 selenium java 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443171/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com