简体   繁体   中英

Unable to find “Button” with Selenium in 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)

Ps: I checked the "similar questions and I haven't found the solution.

Pss: 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 !

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:

  1. 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.
  2. The element can be inside an iframe. If so you have to switch to that iframe in order to access elements inside it.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM