I need to capture all the elements using "findelements" that has the xpath as below, where X can be random numbers like 1,2,3 etc. Except X rest of the Xpath remains constant.
//*[#id='cobCustPgmEditTreePopup']/div[4]/div[3]/div/div[X]/div/span[2]
Please help with solutions on how can I do this?
Just don't specify the index in the first place:
//*[#id='cobCustPgmEditTreePopup']/div[4]/div[3]/div/div/div/span[2]
Hi you can do it like below
x = randomNumber // say 1,2,3,4......
//your xpath
//*[#id='cobCustPgmEditTreePopup']/div[4]/div[3]/div/div[X]/div/span[2]
just brake ur xpath in two parts
String myfirstXpath = "//*[#id='cobCustPgmEditTreePopup']/div[4]/div[3]/div/div[";
String mysecondXpath = "]/div/span[2]";
driver.findElement(By.xpath(myfirstXpath + X + mysecondXpath)).click(); // any action you want
Hope this helps what you are looking for.
Related
I am learning how to write Telegram bots.
The idea of a bot
The user writes a keyword related to the design and parsing the site I need and finds articles that have this keyword.
For now, I just want to display the titles of all the articles that are on the site. The problem is that when I get all the elements
Document doc = Jsoup.connect("https://my-dom.design/dizajn-interera/")
.userAgent("Chrome/4.0.249.0 Safari/532.5")
.referrer("https://yandex.ru/")
.get();
Elements article = doc.select("h3.elementor-post__title a ");
Elements description = doc.select("div.elementor-post__excerpt p");
and then transfer the Elements article to the code of the bot itself, which, when responded, gives these Elements article, then they are all in one line. There is no separation of the article titles, they all go in one row, but I want each name to be a new h3 block started on a new line. How can I do this, please tell me?
Bascially, your article looks somewhat like this:
first link
second link
...
As you can see, this will be rendered in a single line.
IIUC, you want something like this:
<h3>first link</h3>
<h3>second link</h3>
<h3>...</h3>
You can wrap each title link inside an h3 element like this:
Elements article = doc.select("h3.elementor-post__title a ");
article = new Elements(
article.stream()
.map(e -> e.wrap("<h3></h3>").parent())
.collect(Collectors.toList())
);
But in your case, this is madness. A much simpler solution is to alter the query to search less deep. In other words, change:
Elements article = doc.select("h3.elementor-post__title a ");
to:
Elements article = doc.select("h3.elementor-post__title");
Example A:
I need to make below object as dynamic as possible, in order to have robust/flexibility. this is an upload button, but the value of element tend to change for time being:
xpath="//input[#id='**j_idt162:input**'] ,
so i tried below :
xpath="//input[#id='j_idt[0-9],{1,4}:input']
Example B:
i have lists of caseIDs, i only need to get one of it. doesnt matter from the top or down. instead of using static below
xpath = "//a[contains(.,'3131')]")
i tried this
xpath = "//a[contains(.,'^[0-9]{1,5}$')]"), "index:=0"
none of above is working, Example A, i tried to only give 4 digit number but range is dynamic.
Example B, I'm trying to let it pick up only first one link with with limited to range 5, for instance (12345)'
Thanks in advance for answering
HTML node example
It's not possible to use Regex in XPath 1.0, and most browsers support XPath 1.0 only.
You'd better use prefix and suffix in class or id. Then use start-with() and end-with() or contains() to find the element.
For example,
//*[starts-with(#id, 'sometext') and ends-with(#id, '_text')]
Check this answer.
So, the full String that I'm trying to read is:
Members online: (0/0):
Members offline: (0/0):
While I think I could substring it to get the 0, it can change it's size, because it's an int... Is there any way I could get the integers even if they change?
Thanks.
EDIT: I also want to only get the first integer of "0/0"... Any ways I could do this? I know I could use regex but I don't know if it's the best way to do it...
You should use indexOf and lastIndexOf for that purpose.
int n1 = Integer.parseInt(t.substring(t.indexOf('(') + 1, t.indexOf('/')))
That variable (n1) will save the number itself, if you want just the position you should use next one.
int pos=t.indexOf('(') + 1
I am performing my automation in real IOS device.
In one of the aspect I have to automate a picker wheel
xpath:
//UIAApplication[1]/UIAWindow[1]/UIAPopover[1]/UIAPicker[1]/UIAPickerWheel[1]
I would like to select a random value from picker. I can able to do the by sending static value using SendKeys. Instead of the I want to select a random value.
Can I please know how can I do that using java??
Two ways to do this :
Randomise the UIAPickerWheel[1] index while you are using the x-path to access the elements.
You can put all possible static values into an ArrayList and use a random element from amongst them to input using the sendKeys() in way some-what like this :
ArrayList<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2"); // so on for all your values
Random randomizer = new Random();
String random = list.get(randomizer.nextInt(list.size()));
element.sendKeys(random);
Not sure if anyone would still need this. But just in case this could help somebody out there...
Here's an adaptation of the code in Nexial Automation:
val index = RandomUtils.nextInt(0, 7)
// index = 3
val picker = driver.findElement(By.xpath("//XCUIElementTypePickerWheel"))
val currentPickedValue = picker.getAttribute("value")
val pickerId = picker.id
// if picker already has selection, we'll swipe to top of the dropdown first
if (StringUtils.isNotBlank(currentPickedValue))
driver.executeScript(
scriptScript,
mapOf<String, Any>("element" to pickerId, "direction" to "down", "velocity" to 250))
// magic number 25 (per dropdown option)
val scrollAmount = 25.0 / picker.size.height
for (i in 0 until index)
driver.executeScript(
scriptSelectPickerValue,
mapOf<String, Any>("element" to pickerId, "order" to "next", "offset" to scrollAmount))
The code is written in Kotlin. If you know Java/JavaScript/C#, then the above should be reasonably comprehensible.
The magic number is derived via some online research and trial-and-error. Seems to work fine so far.
I randomized index as an integer between 0 to 7. Feel free to change index to something else.
This code has only been tested on XCUITest driver.
Consider moving your view interaction methods to Appium iOS Gestures which does pretty much the same as you did in your script but 'from the box'
Pickerwheel gesture
As of now I am getting the count of the number of matching results using listChanges.size() . How do I directly get the count without loading getChanges in the list?
By getChanges = By.xpath("//td[contains(#class,'blob-code blob-code-addition') or contains(#class,'blob-code blob-code-deletion')]");
List<WebElement> listChanges = driver.findElements(getChanges);
I found this(Count function in XPath) and I tried the below which does not work!
Integer getCount = By.xpath(count("//td[contains(#class,'blob-code blob-code-addition') or contains(#class,'blob-code blob-code-deletion')]"));
Looks like I have to do something like this.
Integer getCount = By.xpath("count(//td[contains(#class,'blob-code blob-code-addition') or contains(#class,'blob-code blob-code-deletion')])");
But the right hand side returns an object of type By
As alex says, size() is the way to go. However I do have another suggestion for you.
Even though the proper way to find the element counts is to use WebDriver api with findElements() as per my knowledge. Another way is to execute javascript by using executeScript() and with proper script. I am not sure if javascript and xpath can be mixed together to accomplish this since xpath execution through javascript is not multi-browser right now. See this. However, I do think using cssSelector with javascript can make it lot easier to accomplish. See the following code :
String cssQuery = ".blob-code-addition, .blob-code-deletion";
String script = "return document.querySelectorAll('" + cssQuery + "').length;";
Object count = ((JavascriptExecutor)driver).executeScript(script);
System.out.println(count);
Print
26
You cannot get the count using XPath, because an xpath expression in selenium has to correspond to an actual element on a page.
The way you are doing it via findElements() + size() is how you have to count elements using the selenium webdriver.