[英]How to Search using wild card entries in Java Selenium
我經歷了每個相關的問題,但似乎沒有一個提供正確的解決方案。 我正在編寫一個程序,使用硒代碼從Facebook獲取朋友列表。 為此,我使用for
循環向下滾動頁面並使用tagname
關鍵字從中獲取朋友的名稱鏈接文本。 同時,我得到了共同的朋友以及每個朋友擁有的朋友數量,結果也得到了。
我注意到每個朋友ID都以js_X
開頭,其中X可以是任何值。 我需要知道如何使用任何通配符搜索/正則表達式來搜索並獲取結果。
下面是我的代碼(我知道它的編程不好,但是請幫助我):
-用於在此處打開Facebook並到達Friendlist頁面的代碼-
WebElement box = d.findElement(By.xpath(".//*[@id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul")); **//This find the first box containing friend list**
List<WebElement> FinalList = box.findElements(By.tagName("a")); **//All Names are fetched and added in Final List**
jse.executeScript("scroll(0, 2500)"); **//Scrolled below to second box**
Thread.sleep(15000L);
box = d.findElement(By.xpath(".//*[@id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul[2]"));**//This finds second box and fetches all the friends name from that box**
List<WebElement> IntermediateList; **//Temporarylist created**
for(int i=3,k=7; i<17||k<20; i++){
if(i<17){
box = d.findElement(By.xpath(".//*[@id='pagelet_timeline_app_collection_100000641984658:2356318349:2']/ul["+i+"]"));
if(box.isDisplayed()){
IntermediateList = box.findElements(By.tagName("a"));
FinalList.addAll(IntermediateList);
jse.executeScript("scroll(0, "+k+"000)"); **//This is for increasing scroll everytime as sometimes box is further down**
Thread.sleep(15000L);
k++;
}else{
break;
}
}
FinalList.addAll(IntermediateList);
我想使用"id"
代替朋友名tagname("a")
來查找朋友名稱鏈接文本,並在其中使用正則表達式/通配符進行搜索。
例如: IntermediateList = box.findElements(By.id("js_(wild card parameter)"));
任何建議都會有所幫助,在此先謝謝!!
您已經在示例中使用了xpath
,所以為什么不使用xpath
查找所需的鏈接?
XPATH:
IntermediateList = box.findElements(By.xpath("//*[contains(@id,'js_')]"));
(找到ID
包含js_
任何元素)
CSS:
IntermediateList = box.findElements(By.cssSelector("a[id^='js_']"));
(找到ID以js_
開頭的鏈接<a href...>
js_
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.