繁体   English   中英

Python Selenium Webdriver selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素

[英]Python Selenium Webdriver selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

我知道我的问题很常见。 然而,尽管我在 Google 和 SO 上进行了大量搜索,但我一直无法找到任何帮助我解决 Python Selenium 问题的内容,并显示上述错误消息“无法定位元素”。 我访问的几乎所有线程都与切换帧有关。 我在我的网站上找不到任何类型的框架标签。

问题是我试图选择一个输入字段,以便我可以 send_keys 但 selenium 无法定位该元素。 该元素具有以下 XPath:

'//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input'

以下是我拥有的代码:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('./chromedriver')  # Optional argument, if not specified will search path.
driver.get(site_url);

time.sleep(5)
delivery_number = driver.find_element_by_xpath('//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input')

driver.quit()

我无法选择上面引用的元素。 xpath 不太可能有错误,因为它是从 chrome 开发工具复制的。

我尝试在 find_element 行之前使用它也无济于事:

element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input')))

我提前为使用截图表示歉意,因为页面源不能准确代表页面,但请参阅下面的元素截图。

在此处输入图片说明

如果有帮助,这是页面源:

<!DOCTYPE html>
<html>
<head lang="zh-cn">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Nestia F&B</title>
    <meta name="renderer" content="webkit">
    <!-- No Baidu Siteapp-->
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <meta name="apple-mobile-web-app-title" content=""/>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="/static/css/ui/amazeui.min.css"/>
    <link rel="stylesheet" href="/static/css/app.css"/>
    <link rel="stylesheet" href="/static/css/patch.css"/>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/videoplayer.css">
    <link rel="stylesheet" href="/static/css/movies.css">
</head>
<body>
<div id="app-root">
    <div style="width:100%;text-align: center">
        <img src="static/img/loading.gif"/>
    </div>
</div>

<script src="/static/scripts/app.bundle.20200907180756.js"></script>
</body>
</html>

感谢您阅读到这里。 一些帮助将不胜感激。 :)

使用WebDriverWait () 和element_to_be_clickable () 并使用xpathcss selector

路径

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="am-form-group item-input"]/input')))

CSS 选择器

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.item-input>input')))

暂无
暂无

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

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