Unable to find "Button" with Selenium in C# - c#

I have got a problem with Selenium code to find a button which has only "Value" and "type", in inspection it looks like this:
<input type="sumbit" value="login" />
Image of inspection
I tried twice but neither line worked for me.
The lines:
1st solution:
driver.FindElement(By.XPath("//button[contains(text(),'Login')]")).Click();
2nd solution:
driver.FindElement(By.ClassName("submit")).Click();
Image with ERROR MESSAGE (second line error)
Can anybody help me, or at least point out what am I missing, because its getting pretty frustrating to find a solution for such common thing, I practiced this on tutorial pages and buttons were never a problem.
Please. (Sorry for my English)
P.s.: I checked the "similar questions and I haven't found the solution.
P.s.s: Guys, there is another one which I didnt try yet but I have 3 different lines of code, do you think one of them will work:
There is drop-down list and I want to select the last thing in the list...
driver.FindElement(By.XPath("//*[contains(., 'Process Data >>')]"));
driver.FindElement(By.Id("pdatasub")).Click();
driver.FindElement(By.XPath("//div[text()='Process Data >>']")).Click();
Inspection of the Drop-down list
Code for the opening of the last "button" in the drop-down list:
driver.FindElement(By.XPath("//div[text()='Final Values']")).Click();
enter link description here
Thanks guys for help !

You are using the wrong locator :
try this instead :
driver.FindElement(By.XPath("//input[#type='submit' and #value='Login']")).Click();
or
With ExplicitWaits
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#type='submit' and #value='Login']"))).Click();

Your locator is wrong.
You can use this:
driver.FindElement(By.XPath("//button[#type='submit']")).Click();
or
driver.FindElement(By.XPath("//button[#value='login']")).Click();
or
driver.FindElement(By.XPath("//button[#value='login' and #type='submit']")).Click();
CSS Selector can be used as well similarly.
Also there are several possible issues:
You should add a wait before accessing that element. Otherwise you are trying to find an element while page is still not loaded. Expected conditions are the preferred way to do this with.
The element can be inside an iframe. If so you have to switch to that iframe in order to access elements inside it.

Related

Element with only class name and repeats multiple times on the page. Selenium c#

I have an element which has only class name as it’s locator or property which can be used for it to locate. I have tried multiple different solution but it doesn’t click on the right one or doesn’t do anything at all. I have tried using X-path but that doesn’t help either because it’s not reliable.
<a class=“Icon-shadows”
<i class=“fa fas.fw fa-info”
::before
I>
a>
This information button appears more then 25-40 times on a page if you click on it you get information about The content.
I can add some more information if need be. Any help suggestions will be much appreciated.
Try saving them to a list, and click on the specific one you need
IList<IWebElement> infoBtn = driver.FindElements(By.CssSelector("a[class='Icon-shadows']"));
infoBtn[4].Click();

FindViewByID() Can't find my ID (2 activities)

I am new to Android programming and I have a little issue.
So I have 2 Activities and 2 layouts.
The problem is that I have a button in my second activity and when I try to declare it FindViewById() can't find the id from the second layout. I re-built the app and I double checked the IDs - they match but simply the function FindViewById() does not find my button's id.
Example:
I have a button in my second layout(second activity? i don't know what is correct to say, as I said im new to android programming)
The button ID is = LoginButton, When I go to my second activity and type
Button LoginButton FindViewByID(Resources.Id.LoginButton);
The function does not find it :/
Im so confused. Please help me
This is because the Resource.Designer can not be created successfully and that is because you have errors in your Layout, so always be aware to check if all properties are defined good.
In MenuList.axml:
Change android:textColor="000" to android:textColor="#000" (most likely this is the problem)
android:layout_height="210.5dp" do you really need those .5 dp? I suggest to put it to android:layout_height="210dp"
Remove android:layout_marginBottom="0.0dp" the margin is anyway 0dp.
After doing this changes Clean and Rebuild the project and see if the error disappear.
To track this kind of errors put your Build in diagnostic so you can
see what is making a problem.
Have you declared the button in the XML file of the second activity? If you have only declared it in the first activity, then findviewbyid won't show an error since there exists an element with that ID. But it won't run (will throw an error) simply because it won't be able to locate that specific element in your (second) activity.
Check if your button's ID is named "#+id/LoginButton"
The correct spelling should be findViewById(R.id.LoginButton) not the way you typed in your question.
Or Are you trying to get the View out of a Fragment?
Try getView().findViewById(...);
if that not works, give us some code, sounds like a typo somewhere.

WebDriver - element is not clickable Chrome

I have following problem. I run test on Firefox and Chrome. On Firefox test run correctly but on Chrome SauceLabs give a message:
unknown error: Element is not clickable at point (717, 657). Other
element would receive the click: <div class="col-md-9 col-sm-12"
style="margin-top:8px;">...</div> (Session info: chrome=36.0.1985.125)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
I choose element by unique css selector in both test in the same way:
driver.FindElement(By.CssSelector("button.btn-xs:nth-child(1)")).Click();
Any ideas what is wrong here?
I am assuming that you have the correct element you need, ie the XPath is correct.
Here are few ways out:
Try to Click on the parent element instead.
Try .Submit() instead of .Click()
Try to execute the JavaScript that will be executed on the OnClick event of the element you are trying to click.
I have used the 3rd way with success all the time.
Another one
Do a .SendKeys(Keys.Enter) on that element (or a Space key)
Since you've tagged the question as Google-Chrome too - I suppose that this is happening mostly with ChromeDriver. I had the same issues with one of my previous projects (Asp .Net MVC). I found that when some elements are not visible for this Driver if they are not in the screen_visible_area. Please note that they are loaded (HTML, CSS3, JS etc.) properly.
So after a lot of reading and testing, I found that my workaround is simply scroll to the WebElement - so it is in the visible part of the screen. Actually this issue was not for all elements and I didn't find better solution for it.
unknown error: Element is not clickable at point (..., ...)
Is not descriptive error for this case, because like you I also thought that is Selector-related.
Just to be full answer - I had the same problems with IEDriver too. My implementation was to use the Browser scroll down/up options and just "send the screen" where the problematic element is.
Simple JSExecutor code that you can use:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(110,350)", "");
or
jse.executeScript("scroll(0, 250);");
or
driver.executeScript("window.scrollBy(110,350)", "");
Other topic-related useful resources are here.
Update
When it comes to the .sendKeys() I also used the browser accessibility features. All you need to do is just count how many TAB clicks your test need in order to get to the targeted web_element. Then just call .click().
Try this simple code:
element.sendKeys(Keys.TAB);
or
element.sendKeys("\t")
or
Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()
I realize this is a super old question, but it came up while searching a nearly identical problem in the present day. After attempting many of the fixes described here and getting new exceptions for my trouble (mostly stale element and http request timeouts) I stumbled across this issue on Selenium's GitHub.
As described in the post, Chrome had advanced beyond the abilities of my version of chromedriver.exe--my v2.30 driver had known issues with clicking elements due to changes in Chrome v61 scrolling mechanics. Updating to the latest chromedriver.exe solved all my problems.
tl/dr: ensure your version of chromedriver is compatible with the version of Chrome being tested.
I was getting issue that login button is not clickable in chrome even xpath was correct. after browsing many sites, i came to the solution - use .submit() instead of .click() and it worked perfectly.
driver.findElement(By.xpath("//button[#id='loginBtn']")).click();
driver.findElement(By.xpath("//button[#id='loginBtn']")).submit();
If you're doing anything complicated in your CSS, Chrome can get confused (this has happened to me) and think that the element you're trying to click is covered by another element even though this is not the case.
One way to resolve this is to help Chrome understand the situation correctly by adding z-indexes (you'll need to add relative or absolute positioning also) to unambiguously place the correct element on top of the other.
For me, it was creating this command instead.
driver.FindElementByXPath("id('gender1')").SendKeys(Keys.Space);
For my case, I was interacting with radio control.
I have had this problem on FF. This issue happens when your field is not in the view area. The slick way to resolve this issue is to zoom out your browser:
TheNotClickableField.SendKeys(Keys.Control + "-" + "-");
you might want to zoom out more or less according to your page size.
I.
If the button is on the bottom of the page, the following code could be used to get you to the bottom via JavaScript from where the click can be executed:
(driver as IJavaScriptExecutor).ExecuteJavaScript("window.scrollTo(0,document.body.scrollHeight - 150)");
The same action could be done via C# with the Actions class provided by Selenium.
This will get you to the bottom of the page----->
new Actions(Driver).SendKeys(Keys.End).Perform();
Keys.End could be switched with Keys.PageDown // Keys.Space
II.
If you want to get to the exact position of the element you can:
1.Get the element's Y location---> var elementToClick = driver.findElement(By.{Anything}(""));
2.Execute the following JS---> (driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,{0})", elementToClickYLocation.Location.Y));
3.Click the element---> elementToClick.click();

Selenium: Finding page element in WordPress

Ok, this should be easy, but I cannot seem to figure it out:
How do I identify and access the new post area (from the Dashboard) in WordPress, using selenium in VS/.Net?
I can access the title field easily by ID, like this:
Driver.Instance.FindElement(By.Id("title")).SendKeys("Sometitle");
But, looking at the page source, I cannot figure out how to access the post body.
In recent versions, I believe, there was an iframe, and it could be accessed like this:
Driver.Instance.SwitchTo().Frame("content_ifr");
Driver.Instance.SwitchTo().ActiveElement().SendKeys(body);
...but this doesn't work anymore, and looking at the source it seems that this has been changed.
So - does anyone know how to do this in recent versions of WordPress?
EDIT: It turns out that I was wrong; there IS indeed an iframe named "content_ifr". So the new question is: Why doesn't the above code work? It's supposed to switch the focus to the content frame, but it doesn't.
try
Driver.FindElement(By.XPath(""));
to find XPath in google chrome right click on element->Inspect element, then click on "Copy XPath".
Hope it helps!

Text property on selenium web element is empty even though I can inspect it with Visual Studio

Without posting pages of C# code and markup, has anyone got a reason why this code
var link = _driver.FindElement(By.Id(field + "Field"));
var id = link.GetAttribute("id");
var text = link.Text;
given this markup
<a id="ForenameField" href="/MyUrl/MyFolder/MyId">3 errors</a>
Assigns an empty string to the text variable, but if I put a breakpoint on the second or third line and inspect the link variable, I can see the inner text of the element against the Text property on the inspector, it reads "3 errors", but the value of text is an empty string. It is not hidden, I can see the text if I add a watch or use quickview, any ideas?
Ok, it's my bad. Using jquery to toggle class on the div that contains the html in the question, meant that although users see the div appearing, the class that hides the div is still in the tag. A bit like this
<div class="hideThis showThis"><!-- my elements /--></div>
This makes it so that Selenium is right not give me a text value. It is strange however that the Visual studio debugger thinks that there should be a value. Visual Studio seems to go with what I can see, but Selenium is more pedantic about the hideThis class being there.
I go with the idea that if you can't see it you can't interact with it, so it is worth looking up the html graph from the element you expect to have a value to see if any class is present which would hide your element.
Feel free to recommend that I delete this rather obvious wisdom.
I know this was posted over a year ago, but I had this exact problem too and came across this thread. I was able to solve it by just waiting for the DOM to load--some elements aren't visible until the DOM is updated. So just putting Thread.Sleep(6000) or whatever after navigating to the page got it to work for me.

Categories

Resources