I need to catch the last xpath element generated (In this case the number 633597015500042325 which is randomly generated and keeps changing after each run).
I understand the method Last() is used to catch the last element
Related
I've been looking for the last couples days for a good solution for this problem. but can't seem to find one.
I have a Jquery script that has to loop through each element get a value and then do an Ajax call, however this is thousands of elements so i would like for the script to go element by element and only move to the next one once the previous action has finished, i tried asyc but that does not seem to wait properly and causes the page to freeze and miss a lot of elements and cause errors.
what advise/script would you suggest i try or look into?
I found a solution to my problem, but it's probably very specific, since i have unique ID's for each element what i did was:
first i defined an empty array and a variable for counting
on the initial click for each loop placing each SKU in an array, once done execute the ajax function
ajax function get's the index of current count then executes function
then i +1 the count and if it is less than the length of the array call ajax function again
i tried to explain it as open as possible in case someone has the same sort of situation, but you can always find a way to assign a random value to your element to somehow identify it
internet.find(:xpath, '/html/body/div[1]/div[10]/div[2]/div[2]/div[1]/div[1]/div[1]/div[5]/div/div[2]/a').text
I am looping through a series of pages and sometimes this xpath will not be available. How do I continue to the next url instead of throwing an error and stopping the program? Thanks.
First, stop using xpaths like that - they're going to be ultra-fragile and horrendous to read. Without seeing the HTML I can't give you a better one - but at least part way along there has to be an element with an id you can target instead.
Next, you could catch the exception returned from find and ignore it or better yet you could check if page has the element first
if internet.has_xpath?(...)
internet.find(:xpath, ...).text
...
else
... whatever you want to do when it's not on the page
end
As an alternative to accepted answer you could consider #first method that accepts count argument as the number of expected matches or null to allow empty results as well
internet.first(:xpath, ..., count: nil)&.text
That returns element's text if one's found and nil otherwise. And there's no need to ignore rescued exception
See Capybara docs
//div[text()='abc']
It gives sometime 10 count, next time 20 and so on.
How can I find the last element in count even if the count changes dynamically?
Welcome to SO. You have to use last() xpath function to return a number equal to context size of the expression evaluation context, meaning it will get the size of the matching elements and you can access the last item by using the below standard xpath notation.
Rather specifying [1] or [5] you are passing the last item number.
(//yourxpath)[last()]
In your case the xpath will be
(//div[text()='abc'])[last()]
Here is the link with more explation
https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256083(v%3Dvs.100)
I am trying to locate Ajax control (mouse over) on Amazon home page for sign in.
WebElement element = driver.findElement(By.xpath("//*[#id='nav-link-yourAccount']"));
however this element locator works for some time and other times its not finding element and script is failing.
I have noticed that Xpath of this element is changing sometimes to //*[#id='nav-link-yourAccount']/span[1], there is no any other unique identifier which can be used to locate this element.
Could you please let me know how to resolve this variable xpath issue.
If you fail to find an element at one of the xpath values, you could try searching for the other xpath value. You could use ExpectedConditions to wait for a certain period of time for an element to exist. If that time elapses and the element is not found, then use the second locator to find the element. This assumes that the xpath is only changing between these two known values. Also, once you locate the element, you might want to make some assertions about the other properties of the element to further ensure you've found the element you're looking for.
Here's a post about waiting for an element:
Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?
I have a very poorly coded JSP that I am trying to run automation on. There are a series of checkboxes with names (no IDs) of "delete[x]" where X is the item number of the item populated. I am trying to select all the checkboxes so I can delete every entry. Here is what I have
check_boxes = []
check_boxes.push(#browser.checkbox(:xpath, "//input[contains(#name,'delete')]"))
puts check_boxes.size
check_boxes.each do |check_box|
check_box.set
The problem with this is it only selects the first instance (node) that matches the xpath to dump into the array. I know I can iterate through the xpath adding an index to the node, and then put a rescue in that drops me out when the index goes out of bounds, but that seems like the dirty way to do it.
I know there is an "as" tag that gets a set of anchors and i was wondering if there was a method like that for taking the whole selection of checkboxes
I don't think the problem is the xpath itself. It is the #browser.checkbox that is causing only the first checkbox to be returned.
If you want all matching checkboxes, you should use (notice the plural):
#browser.checkboxes
Note that checkboxes returns a collection of checkboxes. Unless you are doing something really fancy, you usually do not need to convert it to an array.
You can simply do:
#browser.checkboxes(:name => /delete/).each do |checkbox|
checkbox.set
end