How to find last() element in webdriverIO with find by elements $$ - webdriver-io

Below is example fo Protractor which has last() function to filter
admin.elements.executions.last().element(by.css('[value="REQUIRED"]'))
How to do in webdriverIO
need to find last element in $$

You can use :last-child details here or you can follow this link https://webdriver.io/docs/api/browser/$$

Related

locating xpath with starts with and ends with

Following xpath I have copied from the browser(inspect)
# // *[ # id = "customize-coverage--customize-your-quote--propertyDamage-1--info-title"]
I am trying to read the element but its just not able to locate it with the follwoing xpath
Is there anyway I can locate the above element by selenium? Please help. I am not sure why the belwo is not working to locate the web element by driver
return self.page_element(By.XPATH,
'//*[starts-with(#id,"customize-coverage--customize-your-quote--propertyDamage-") and ends-with(#id,"-info-title")]/span').text
I tried to use contains and regex also. Nothing seems to work.
Note: I am using python
ends-with() requires XPath 2.0, and although that's now nearly 14 years old, Selenium doesn't implement it yet. You have to use a combination of substring() and string-length().

What does contains(., 'some text') refers to within xpath used in Selenium

I am new to selenium coding, and I am seeing several xpaths that contain (.,'followed by something') what does the ., refer to?
The . character within the xpath is the short form of text()
As an example if an WebElement is represented within the DOM Tree as:
<span>Use this payment method</span>
Effective Locator Strategies will be:
xpath 1:
//span[text()='Use this payment method']
xpath 2:
//span[contains(., 'Use this payment method')]
Reference
You can find a couple of relevant discussions in:
How to locate the button element using Selenium through Python
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text

selenium webdriver_Is there any other way to find out the count of webelements present in a webpage without using "findElements()" method?

Is there any other way to find out the count of webelements present in a webpage without using "findElements()" method?
This Question is asked in Interview. So I would like to know whether it is possible to get the count of Webelements without using findElements().
Update to #mate and #custom answer,
using xpath, to extract the number.
driver.executeScript(() => $x('count(//div)'));
if there is 25 div elements, will return 25 as a number.
Here's one way:
Go to DevTools
Enter $x('//*') to console
You will get the number of HTML elements
You could use WebDriver.prototype.executeScript which lets you execute some JavaScript code. That code can either be a function or a string:
driver.executeScript(() => document.querySelectorAll('*').length);
PS: I'm using the JS flavour of selenium-webdriver.

Getting org.openqa.selenium.InvalidSelectorException only for Chrome

I am tring to find a button in a webpage using find elements, the page can contain one of the below button ID's.
driver.findElements(By.xpath("//*[contains(#id,'topBtn')]"))
driver.findElements(By.xpath("//*[contains(#id,'WSMplasticTop')]"))
driver.findElements(By.xpath("//*[contains(#id,'bottomApplyBtn')]"))
The above code is working as expected when i use the Firefox Driver, where as getting the below error when i run in Chrome Driver.
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(#id='bottomApplyBtn')] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(#id='bottomApplyBtn')]' is not a valid XPath expression.
Just wanted to know whether i have done any mistake
Try to use
'//*[contains(#id,'bottomApplyBtn')]'
instead of
'//*[contains(#id='bottomApplyBtn')]'
When you are using contains method in your XPath expression, you basically are using an inbuilt method to find the element.
This method takes 2 parameters.
First the tag you in which you want to search.
Second is the actual text value which you are looking for inside the above tag.
Basically you have to call the method with 2 parameters and that 2 parameters should be comma separated.
So //*[contains(#id='bottomApplyBtn')] is wrong you should instead remove this = sign.
//*[contains(#id, 'bottomApplyBtn')]
|_______|____________________ Parameter 1
|__________________________________Parameter 2
Hope it helps!

Not able to identify a lenghty linktext in selenium webdriver

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution
Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click
try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.