简体   繁体   English

无法在网站上找到元素,我尝试了所有定位器,但出现错误“没有这样的元素”。 我是自动化的自学初学者

[英]Unable to locate elements on a website , i tried all the locators but I get the error "No such element" . I am a self taught beginner to Automation

Element HTML:元件 HTML:

<a href="/cacApp/Main/INBOX" title="View the Inbox" target="main">Inbox</a>

What I tried:我尝试了什么:

driver.findElement(By.cssSelector("a[text='Log out']"));

then然后

driver.findElement(By.xpath("//a[.='Log out']"));

Element snapshot:元素快照:

在此处输入图像描述 HTML HTML

driver.findElement(By.linkText("Log out"));

Something like that should work, you should provide the page html for a better response.类似的东西应该可以工作,您应该提供页面 html 以获得更好的响应。

My response is based on the fact that in your first try you are saying text='Log out'.我的回答是基于这样一个事实,即在您第一次尝试时您说的是 text='Log out'。 findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element. selenium 中的 findElement 与我使用的 locatorStrategies (By.cssSelector/By.linkText ...) 一起使用 (linkText) 在页面 () 中找到的所有锚标签中搜索并分析标签内的文本,如果文本匹配驱动程序将找到该元素。

Let me know if it works, otherwise provide me the web page html snippet.让我知道它是否有效,否则请提供 web 页面 html 片段。

I've seen the actual screen, you must change Log out with Inbox我已经看到了实际的屏幕,您必须使用 Inbox 更改 Log out

    driver.findElement(By.linkText("Inbox"));

Given the HTML:鉴于 HTML:

<a href="/cacApp/Main/INBOX" title="View the Inbox" target="main">Inbox</a>

You need to take care of a couple of things here as follows:您需要注意以下几点:

  • Within cssSelector doesn't supports the :contains("text") methodcssSelector中不支持:contains("text")方法
  • Within xpath for exact text matches you need to use text()xpath中,您需要使用text()

Solution解决方案

To identify the element you can use either of the following locator strategies :要识别元素,您可以使用以下任一定位器策略

  • Using linkText :使用链接文本

     WebElement element = driver.findElement(By.linkText("Log out"));
  • Using cssSelector :使用cssSelector

     WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
  • Using xpath :使用xpath

     WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and @title='View the Inbox']"));

暂无
暂无

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

相关问题 我尝试了所有可能的方法使用Webdriver来定位元素,但收到错误消息“无法定位元素” - I have tried all possible ways to locate an element using Webdriver, but getting an error “Unable to locate Element” Java Selenium - 无法定位元素 - 尝试了所有可能的定位器 - Java Selenium - Unable to locate an element - tried all possible locators 我找不到元素 - I am unable to locate an element 我无法使用“字段定位器”发送电子邮件给收件人 - I am unable to send email to recipient with Field Locators 当我运行以下代码时,无法找到元素错误 - Unable to locate an element error is coming when i am running the below piece of code xpath 测试并正确,但我收到错误:没有这样的元素:无法定位元素 - xpath tested and correct but i recieve the error : no such element: Unable to locate element 我试图运行这段代码,它在不抛出错误的情况下运行,问题是它不执行循环我只是一个初学者,所以请 - I tried to run this code it runs without throwing error and the question is to It doesn't execute for loopsI am just a beginner so please 我正在制作一个提醒应用程序,并且尝试使用领域。 我正在使用getInstance()。这是我得到的错误 - I am making a reminder app, and I have tried to use realm. I am using getInstance().This is the error I get 没有这样的元素:无法找到元素:当我使用elementToBeClickable方法时 - no such element: Unable to locate element: when I use elementToBeClickable method 我如何从网站上获取所有元素 - How can i get all the elements from a website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM