简体   繁体   English

org.openqa.selenium.NoSuchElementException:无法找到元素:-搜索字段

[英]org.openqa.selenium.NoSuchElementException: Unable to locate element:- Search field

I am trying to search in learning search field in linkedin ( https://www.linkedin.com/learning/me?trk=nav_neptune_learning ) 我正在尝试在linkedin( https://www.linkedin.com/learning/me?trk=nav_neptune_learning )的学习搜索字段中进行搜索

  1. Login to linkedin 登录到linkedin
  2. Click on "Learning" link(Top right corner) 点击“学习”链接(右上角)
  3. I am sending tab keys and reaching the "search" field 我正在发送Tab键并到达“搜索”字段
  4. Finally find the "search" field using xpath, and I am sending search keyword (.sendKeys("Python") 最后使用xpath找到“搜索”字段,然后发送搜索关键字(.sendKeys(“ Python”)

Selenium with java: 硒与Java:

driver.findElement(By.xpath("//div[@class='search-container']/descendant::input[@type='text']")).sendKeys("Python");

Till step 3, it's working, search field is highlighted. 到第3步为止,它一直有效,搜索字段突出显示。

The final step always fails, with below exception 最后的步骤总是失败,以下例外

org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素:

I tried the below xpath: 我尝试了以下xpath:

//div[@class='container global-nav__container']//div[@class='search-container']/artdeco-typeahead[@id='ember863']/div/input[@type='text']
//div[@class='search-container']/descendant::input[@type='text']
//div[@class='search-container']/artdeco-typeahead[@id='ember863']/div/input[@type='text']
//div[@class='search-container']/artdeco-typeahead/div/input[@type='text']

在此处输入图片说明 Interesting thing is with all the above xpath, I am able to find the element in the broswer, but the same xapth doesn't work in selenium code. 上面所有的xpath都有意思,我可以在浏览器中找到该元素,但是相同的xapth在硒代码中不起作用。

My code -Step 4 我的代码-步骤4

@Test
    public static void search() throws InterruptedException, AWTException
    {

        Robot robot = new Robot();
        for ( int i=0; i<=5;i++)
        {
        robot.keyPress(KeyEvent.VK_TAB);
        Thread.sleep(2000);
        }
        driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Python");
    }

NoSuchElementException happens generally in 2 scenarios. NoSuchElementException通常在两种情况下发生。

  1. Webelement locator is wrong. Webelement定位器错误。 (which doesn't seems to be the situation in your case) (您的情况似乎并非如此)
  2. Your are trying to find the webelement even before it is loaded on the page.(This seems to be your problem) 您甚至在将Webelement加载到页面之前都在尝试查找它。(这似乎是您的问题)

You are getting the "NoSuchElementException" because your are entering the text even before the webelement on the page has completely loaded. 之所以得到“ NoSuchElementException”,是因为您甚至在页面上的网络元素完全加载之前就输入了文本。

I would suggest you wait till the webelement that you are looking for to get loaded first and then use sendkeys. 我建议您等到要查找的Web元素首先加载后再使用sendkeys。 Use can use WebDriverWait class 使用可以使用WebDriverWait

so the refactored code should look somewhat like this. 因此,重构后的代码应该看起来像这样。

//Initializing the 'wait' with a 30 seconds deplay before it throws a NoSuchElementException
WebDriverWait wait = new WebDriverWait(driver,30);

wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("input[placeholder='Search for skills, subjects or software']"));

driver.findElement(By.cssSelector("input[placeholder='Search for skills, subjects or software']")).sendKeys("Python");

下面的代码将为您工作:

driver.findElement(By.cssSelector("input[placeholder='Search for skills, subjects or software']")).sendKeys("Python");

暂无
暂无

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

相关问题 org.openqa.selenium.NoSuchElementException:无法找到元素错误 - org.openqa.selenium.NoSuchElementException: Unable to locate element error org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: 无法找到元素:org.openqa.selenium.NoSuchElementException - Unable to locate element: org.openqa.selenium.NoSuchElementException webdriver org.openqa.selenium.NoSuchElementException:无法找到元素: - webdriver org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:无法找到表格标签内的元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: not able to locate the element inside table tag 代码中的 Selenium Fluent Wait Implementation 仍然给出“org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” - Selenium Fluent Wait Implementation in the code still gives “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element error using Selenium through Java org.openqa.selenium.NoSuchElementException:没有这样的元素:尝试使用Java通过Selenium登录时无法找到元素 - org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java 线程“ main”中的异常org.openqa.selenium.NoSuchElementException:无此类元素:无法找到元素:{“ method”:“ id”,“ selector”:“ name”} - Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:“id”,“selector”:“name”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM