[英]How to Search using wild card entries in Java Selenium
I have gone through every related question but none of them seems to provide the correct solution. 我经历了每个相关的问题,但似乎没有一个提供正确的解决方案。 I am writing a program to fetch the friend list from Facebook using selenium code.
我正在编写一个程序,使用硒代码从Facebook获取朋友列表。 To do so, I am using
for
loop to scroll page down and fetch friends name link text from it using tagname
keyword. 为此,我使用
for
循环向下滚动页面并使用tagname
关键字从中获取朋友的名称链接文本。 While doing so, I get mutual friends and number of friends each friend have, also in the result. 同时,我得到了共同的朋友以及每个朋友拥有的朋友数量,结果也得到了。
I noticed that each friend id starts with js_X
where X can be any value. 我注意到每个朋友ID都以
js_X
开头,其中X可以是任何值。 I need to know how I can use any wild card search/Regular expression to search and fetch the results to me. 我需要知道如何使用任何通配符搜索/正则表达式来搜索并获取结果。
Below is my code(I know its poor programming, but please help me): 下面是我的代码(我知道它的编程不好,但是请帮助我):
--code for opening facebook and reaching till Friendlist page here--- -用于在此处打开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);
Instead of tagname("a")
I want to use "id"
to find the friend name link text and use Regular expression/wild card in it to search. 我想使用
"id"
代替朋友名tagname("a")
来查找朋友名称链接文本,并在其中使用正则表达式/通配符进行搜索。
For example : IntermediateList = box.findElements(By.id("js_(wild card parameter)"));
例如:
IntermediateList = box.findElements(By.id("js_(wild card parameter)"));
any Suggestion would help, Thanks in advance!! 任何建议都会有所帮助,在此先谢谢!!
You already used xpath
in your example so why don't you use xpath
to find the desired links? 您已经在示例中使用了
xpath
,所以为什么不使用xpath
查找所需的链接?
XPATH: XPATH:
IntermediateList = box.findElements(By.xpath("//*[contains(@id,'js_')]"));
(Find any element which ID
contains js_
) (找到
ID
包含js_
任何元素)
CSS: CSS:
IntermediateList = box.findElements(By.cssSelector("a[id^='js_']"));
(Find a link <a href...>
which ID starts with js_
) (找到ID以
js_
开头的链接<a href...>
js_
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.