Unable to enter a value in field using Xpath - selenium

I am unable to enter a value in field which is having type as "text" after running the automated script. Please find details below
Field Name: "Spot/Content Name
Xpath value for the above field :
.//*[#id='154']/div[3]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div[2]/input
Automated Script for the above Xpath:
driver.findElement(By.xpath(".//*[#id='154']/div[3]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div[2]/input")).clear();
driver.findElement(By.xpath(".//*[#id='154']/div[3]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div[2]/input")).sendKeys("Rose");
Error Message :
NoSuchElementException: Unable to locate element:
{"method":"xpath","selector":".//*[#id='154']/div[3]/div[1]/div/div/div/div[1]/div/div/div[2]/div/div[2]/input"}
I tried even the html which was shown on the xpath but it was not working.

This is not the way Xpath should be used, please go through the following link: so that you could understand it in a better way.
http://www.toolsqa.com/selenium-webdriver/choosing-effective-xpath/
Also it is always good practice to share you HTML code so that ppl could identify the problem.
Meanwhile your element can be found using CSSSelector also try this--
driver.findelement(by.cssSelector("input[id='154']")).clear();
driver.findelement(by.cssSelector("input[id='154']")).sendKeys("Rose");
Please let me know if this works..!!

Related

Selenium C# - I'm unable to find an element on this page using any of the locators

This was just a random script I made to complete a quiz but I can't seem to access the final element. I want to select the element, click the element and then send some text to the element.
I have tried to access the input box by class name, CssSelector and by XPath.
The website is https://www.16personalities.com/free-personality-test
Here are the XPaths I have tried:
//*[contains(#class, 'email-wrapper')]
//div[contains(#placeholder, 'your#email.com')]
//div[#class="row request-info-wrapper"]
//*[#id='request - email']"
Any help is greatly appreciated as I'm new to the framework and would very much like to know what I'm not understanding about locators! Thank you!
EDIT:
I can't seem to target this element or any of its children:
You have selected wrong tag DIV.Try this following Xpath. All should work.
"//input[#id='request-email']"
Or
"//input[#name='email']"
Or
"//input[#placeholder='your#email.com']"
Your field has a (presently) unique ID of "request-email".
Thus you can simply use, as a CSS selector,
('#request-email')
Then, in you can simply tell Selenium to hit ENTER to save your data. Let me know if you need help doing that.

How to implement //*[contains(text(), '"example"')] to construct an Xpath for dynamic contents for Selenium Webdriver:?

I'm working through Selenium Webdriver, and I've come up to the issue of dynamic objects as a DOM ID. One of my instances gets generated as an ID, something like this:
"//*[#id="888itsAnExampleBoi573"]/div[1]/div[2]",
and I need to click on the button in the example item to Make Stuff Happen.
Because I cannot predict what an objectID will be for my dynamic content, however, I would like to be able to do this, instead:
//*[contains(text(), 'example')]/div[1]/div[2].
I've tried to do this, but I'm returned a strange error:
Caused by: org.openqa.selenium.InvalidSelectorException: invalid
selector: Unable to locate an element with the xpath expression
//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div
because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document':
The string '//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div'
is not a valid XPath expression.
On a different element that is a hyperlink with text elements, I've been able to use contains(text()) to solve things, so I believe I've formatted this correctly.
I've tried a few different things to solve this issue, but am at somewhat of a loss as to how to solve this. Does anyone have any ideas or resources to point me towards? Or better yet, a solution?
This error message...
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(), 'example')]/div/div/div[1]/div[3]/div because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div' is not a valid XPath expression.
...implies that the Locator Strategy is not a valid XPath expression.
The expressions:
'//*[contains(text(), 'example')]/div/div/div[1]/div[3]/div'
"//*[contains(text(), "example")]/div/div/div[1]/div[3]/div"
Are invalid as '' and "" were present in multiple places within the xpath.
Solution
As an effective alternative dynamic xpath for the following:
"//*[#id="888itsAnExampleBoi573"]/div[1]/div[2]"
will be:
"//*[contains(#id,'itsAnExampleBoi')]/div[1]/div[2]"
It appears that in order to solve this, sometimes you must frame the question. As I had been using a relative XPATH, I had been working under the pretense that the text div itself was where I would be able to access elements that were not the children of my div. They were actually only loosely related!
"//*[contains(text(), 'Flag this')]
//parent::div
//parent::div[contains(#class,'exampleDrop')]
//child::div[contains(#class, 'pointed exampleAction')]"
I ended up installing Chropath and spidering around the DOM, finding the div with the text in it, and using a relative path in this statement. If you're having issues like these, sometimes I would recommend making sure that you're asking the right question.

Unable to locate element in the popup

I am trying to locate those buttons as shown here:
And this is HTML for the same :
I tried with all possible ways but nothing works.
As per the HTML you have shared you can locate the Continue with New button with either the following options :
XPATH
//button[#class='slm-btn slm-btn-AdvanceFlow' and #id='Continue']/span[#class='slm-btn-text']
CSS_SELECTOR
button.slm-btn.slm-btn-AdvanceFlow#Continue > span.slm-btn-text
First, you have to try with id because it is the best way to get it because it is unique.
Otherwise, you can use XPath:
//button[#id='ContinueNew']
//button[contains(#id,'ContinueNew')]

Selenium Webdriver: cssselector

I am trying to do SignIn for this. In it click on 'SignIn' which I have done it successfully. Now when trying to type in Username/Password using Xpath it shows exception which says
Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: element not visible
code is :-
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[2]/input")).sendKeys("test123");
I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? How we can use it by using cssselector?
Thanks for the help in advance.
Go One Level up on finding the relative xpath with
//div[#id='login']/form[#id='load_form']//input[#name='username']
//div[#id='login']/form[#id='load_form']//input[#name='password']
Try this
//For Username
driver.findElement(By.xpath("(//input[#name='username'])[2]")).sendKeys("username test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();
//For password
driver.findElement(By.xpath("(//input[#name='password'])[2]")).sendKeys("password test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();
the above codes are working for me.
There are basically two elements found by provided xpath, and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :-
driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");
Note:- For learning about cssSelector follow this url and for xPath follow this url
I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12. for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression")..
Hope it will help you..:)

Selenium preceding-sibling::text() not working

I am having issues with selenium using the next xpath "./preceding-sibling::text()"
I don't understand why, my first thought was that IE wasn't supporting that xpath statement but it didnt work on chrome neither.
What I am trying to do is to verify if a radio button have a certain text "near" it. For example if a radio button is like this
<div>
<label> Yes <input name='radioBtn'></input></label>
<label> No <input name='radioBtn'></input></label>
</div>
This is a simplified scenario where I need to check the "yes" radio button, so what I am doing is to search for the radiobutton and check if it preceding-sibling::text(), but selenium is cant find any element. I know the Xpath works because I test it on chrome developer tools and it returns the text "Yes".
I can't do something like 'input[1]' because I can't be sure that the first one will be always that have the Yes text.
Any idea why isn't this working on selenium? and if there is any work around?
I got to a work around but is kind of specific to the problem. But let's answer the questions 1 at the time.
Any idea why isn't this working on selenium?
It's not working because selenium don't support text elements, so even when selenium find the element it cant map it to a selenium object, i didn't see it because my code hided the selenium exception. The Exception throw is the next one:
An unhandled exception of type
'OpenQA.Selenium.InvalidSelectorException' occurred in WebDriver.dll
Additional information: invalid selector: The result of the xpath
expression "./preceding-sibling::text()" is: [object Text]. It should
be an element
Is there any work around?
Yes it is. What I did was to run a java script using the selenium IJavaScriptExecutor. With the script I revised the preceding sibling text and return it as a string so if the text was equal to the thing I was looking for (i.e Yes) trhat means that is the radio button I was looking for.
The code looks is similar to this (it can have some sintax errors since I didn't copied from the source):
string script = "function GetPrecedingText(node){"+
"var result = document.evaluate('./preceding-sibling::text()[1]', node, null, XPathResult.STRING_TYPE, null);"+
"return (result==null) ? '' : result.stringValue.trim();}"+
"return GetPrecedingText(arguments[0])";
string result = ((driver as IJavaScriptExecutor).ExecuteScript(script, SeleniumElement)).toString();
Hope someone can find this useful :) and thanks for all that tried to help me :)