首页 > 解决方案 > 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:要在此添加更多细节以使其更易于理解,当我单击按钮时,它会生成实际搜索栏的下拉菜单。因此,如果它最终找到该元素。它给了我这个“消息:过时的元素引用:元素未附加到页面文档”

标签: pythonseleniumselenium-webdriver

解决方案


a不是名称,因此不会导致使用 method 查找元素find_element_by_name。请尝试使用 xpath//span[text()='Search'] 查找元素搜索。请检查(在开发人员控制台或其他浏览器扩展程序中,如 xpath、chropath 等)使用此 xpath 返回了多少元素。如果只有一个元素,你就可以走了。否则,在继续对其执行操作之前,找到一些其他策略来达到所需的元素。


推荐阅读