简体   繁体   English

无法在 C# 中找到带有 Selenium 的“按钮”

[英]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:我在使用 Selenium 代码时遇到了一个问题,无法找到一个只有"Value""type"的按钮,经过检查,它看起来像这样:

<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. Ps:我查了“类似的问题,我还没有找到解决方案。

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: Pss:伙计们,还有另一种我还没有尝试过,但我有 3 行不同的代码,你认为其中一种会起作用吗:

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.XPath("///*[contains(., '处理数据>>')]"));

driver.FindElement(By.Id("pdatasub")).Click(); driver.FindElement(By.Id("pdatasub")).Click();

driver.FindElement(By.XPath("//div[text()='Process Data >>']")).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(); 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. CSS Selector 也可以类似地使用。
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.元素可以在 iframe 内。 If so you have to switch to that iframe in order to access elements inside it.如果是这样,您必须切换到该 iframe 才能访问其中的元素。

You are using the wrong locator :您使用了错误的定位器:

try this instead :试试这个:

driver.FindElement(By.XPath("//input[@type='submit' and @value='Login']")).Click();

or要么

With ExplicitWaits使用ExplicitWaits

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@type='submit' and @value='Login']"))).Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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