python - Selenium 找不到链接的元素
问题描述
我正在尝试制作一个程序来登录我的 depop(二手服装销售应用程序)并搜索用户。到目前为止,我已经设法让它登录。但是,它找不到搜索按钮的元素。
我已经尝试了多种没有的方法,但都没有奏效。请记住,我今天已经开始学习这个,所以我是初学者。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys #allows keyboard to be used
from selenium.webdriver.common.by import By #allow waiting
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as cond
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import TimeoutException
def search():
driver.implicitly_wait(4)
searchbox = driver.find_element_by_name('a')
消息:没有这样的元素:无法找到元素
我只是根据我的操作方式获得此错误消息的变体。
编辑:元素是:
a
< span > Search < /span >
/a
编辑2:要在此添加更多细节以使其更易于理解,当我单击按钮时,它会生成实际搜索栏的下拉菜单。因此,如果它最终找到该元素。它给了我这个“消息:过时的元素引用:元素未附加到页面文档”
解决方案
a
不是名称,因此不会导致使用 method 查找元素find_element_by_name
。请尝试使用 xpath//span[text()='Search']
查找元素搜索。请检查(在开发人员控制台或其他浏览器扩展程序中,如 xpath、chropath 等)使用此 xpath 返回了多少元素。如果只有一个元素,你就可以走了。否则,在继续对其执行操作之前,找到一些其他策略来达到所需的元素。
推荐阅读
- css - React Styled Components:对齐内容不起作用,但对齐项目和 flex-direction 可以吗?
- python - How to split a dict as per certain parts of the keys or extract values only for the keys have similar keywords
- python - 通过 Python 注册 Service Worker 时 serverApplicationKey 无效
- excel - VBA:如何从股票 OHLC 图表的 Y 轴上的值中删除小数点
- docker - linuxserver/openssh-server 设置远程 ssh
- terraform - Terraform:引用未声明的输入变量
- scala - 使用 scala 2.12.10 时 spark 的 pom.xml 依赖项
- javascript - 我如何在 NestJS 中从另一个类扩展一个类?
- regex - 使用正则表达式在标签中查找字符串
- kivy - kivy - name 错误名称 未定义线程