How to get value from Node in Selenium Webdriver and Print in Console? - xpath

I have tried using Webelement to store value but
facing Error like :
[Below is the image of which I want to get value that is K1]
I am Not Getting value through Weblelement as It is displaying Error as
org.openqa.selenium.InvalidSelectorException: The given selector //div[2]/label/strong/following-sibling::text()
is either invalid or does not result in a WebElement.
I just want to store value in variable and print it, I have tried below code but no result :
WebElement Key = driver.findElement(By.xpath("//div[2]/label/strong/following-sibling::text()"));
String vs =Key.getText();
System.out.println("key Name..!!!" + vs);
Suggest Me Opinions

Try with this Xpath to obtain the WebElement:
.//strong[text()='Key']/../..//label
After that with element.getText() you should have the value : K1

Related

Get value of aggregate xpath function in Selenium Java

I'm trying to get the index of a <tr> element based on the contents of its <td> elements in Selenium using xpath.
String xpath = "count(//tr[td[text()='Column Value A'] and td[text()='Column Value B']]/preceding-sibling::*)"
WebElement count = driver.findElement(By.xpath(xpath))
However, I'm getting this exception.
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression count(//tr[td[text()='Column Value A'] and td[text()='Column Value B']]/preceding-sibling::*) because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
Is there any way for Selenium to return the value of an aggregate xpath function?
Thanks in advance for your help!

Unable to locate element by giving variable name inside XPATH

brand_name = 'LACOSTE'
element = browser.find_element_by_xpath("//*[text()= brand_name]")
This is giving error. However, if i do like this:
element = browser.find_element_by_xpath("//*[text()= 'LACOSTE']")
then element is found.
But i dont want to hardcode the value. Please help.enter image description here
The XPath needs apostrophe's around the search-string, so I think you need to concatenate the XPath expression like this
element = browser.find_element_by_xpath("//*[text()='" + brand_name + "']")

vars.put function not writing the desired value into the jmeter parameter

Below is the code which i have been trying to address the below UseCase in JMETER.Quick help is appreciated.
Usecase:
A particular text like "History" in a page response needs to be validated and the if the text counts is more than 50 a random selection of the options within the page needs to be made.And if the text counts is less than 50 1st option needs to be selected.
I am new to Jmeter and trying to solve this usingJSR223 POST processor but somehow stuck at vars.put function where i am unable to see the desired number being populated within the V paramter.
Using a boundary extractor where match no 1 should suffice the 1st selection and 0 should suffice the random selection.
def TotalInstanceAvailable = vars.get("sCount_matchNr").toInteger()
log.info("Total Instance Available = ${TotalInstanceAvailable}");
def boundary_analyzer =50;
def DesiredNumber,V
if (TotalInstanceAvailable < boundary_analyzer)
{
log.info("I am inside the loop")
DesiredNumber = 0;
log.info("DesiredNumber= ${DesiredNumber}");
vars.put("V", DesiredNumber)
log.info("v= ${V}");
}
else{
DesiredNumber=1;
log.info("DesiredNumber=${DesiredNumber}");
vars.put("V", "DesiredNumber")
log.info("v= ${V}");
}
def sCount = vars.get("sCount")
log.info("Text matching number is ${sCount_matchNr}")
You cannot store an integer in JMeter Variables using vars.put() function, you either need to cast it to String first, to wit change this line:
vars.put("V", DesiredNumber)
to this one
vars.put("V", DesiredNumber as String)
alternatively you can use vars.putObject() function which can store literally everything however you will be able to use the value only in JSR223 Elements by calling vars.getObject()
Whenever you face a problem with your JMeter script get used to look at jmeter.log file or toggle Log Viewer window - in absolute majority of cases you will find the root cause of your problem in the log file:

Trying to parse string from a website that gives device status with a value at the end

I'm using ruby and trying to get a value from a string that I received from an URI.PARSE.
Below is what I get back from the URI.PARSE and is in my string. You can see it in the result at the bottom. Q8:0; I only need the Device which in this case is Q8 and the value is 0. The device is always a string but sometimes the value is a string and sometimes a integer. I want to be able to evaluate this result to do events based on the values.
html code>Q8:0;html code
_, device, value, _ = "html code>Q8:0;html code".split(/[>:;]/)

How to assign the text box value to a variable

I wanted to get the t#test.com value from the text box which has introduced as the (By.Id("Email")) and compare with the selectEmail value. I have used the following code, but it doesnot take any value which was saved from the text box in the text box. When I debug it I could get only null value for the foundEmail variable.
var checkEmail = driver.FindElement(By.Id("Email"));
string foundEmail = checkEmail.Text;
string selectedEmail = "t#test.com";
Assert.AreEqual(foundEmail, selectedEmail);
Please help me to assign the t#test.com value from the text box to the given variable called foundEmail.
Thankyou
You are using checkEmail.Text please use following code
string foundEmail = driver.findElement(By.id("Email")).getAttribute("value"))
Try it
Thanks.

Resources