当前位置: 首页 > 知识库问答 >
问题:

如何使用Selenium和Python为下面的html定位元素

莘聪
2023-03-14
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='react-autosuggest__input react-autosuggest__input--open']"}
(Session info: chrome=73.0.3683.103)
<input type="text" autocomplete="off" aria-autocomplete="list" aria-controls="react-autowhatever-1" class="react-autosuggest__input react-autosuggest__input--open" placeholder="From" value="">
//*[@class='react-autosuggest__input react-autosuggest__input--open']   

我希望上面的xpath的输出能够定位selenium中的元素,但实际上我得到了错误。

共有1个答案

汪晟睿
2023-03-14

该元素似乎是一个React元素,因此要定位该元素,您必须引导WebDriverWait使该元素可单击,您可以使用以下解决方案之一:

>

  • 使用css_selector:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.react-autosuggest__input.react-autosuggest__input--open[placeholder='From']")))
    

    使用XPath:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='react-autosuggest__input react-autosuggest__input--open' and @placeholder='From']")))
    
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  •  类似资料:
    • 我试图找到以下元素: 使用selenium webdriver。但我得到的信息是: 消息:没有这样的元素:无法定位元素:{"方法":"xpath","选择器":"//div[@id='terms-modal']/div//[@id='接受条款']//[@onClick="close_terms_window();"]"} (会话信息: chrome=74.0.3729.131)(驱动程序信息: c

    • 因此,我一直在尝试使用selenium在python上定位用户名输入框。我尝试了xpath、id、类名等。 我还知道显式等待元素加载。然而,尽管如此,没有运气。我检查了元素是否在我忽略的iframe中,但找不到它。 这是用户输入框元素。 下面是我为定位元素而编写的代码。

    • 问题内容: 我正在查看的页面包含: 我想获取div中的所有文本,除了中的文本。(我想获得“文本1”,“文本3”和“文本4”)。可能有几个元素,或者根本没有。而且可能有一些元素,甚至一个元素都在另一个元素之中,或者根本没有。 我想通过获取div的所有html源并使用正则表达式删除元素来做到这一点。但是selenium.get_text不会返回html,而只是返回文本(全部!)。 我知道我可以使用正则

    • 我喜欢在我的自动化框架中使用带有FindBy注释的PageFactory来自动定位我的页面对象类中的元素。 我有一个WebElement,我需要能够指定几个不同的定位器。我以为FindBys是我的解决方案,但显然这不是它的工作方式。它相当于Driver.FindElement(option1).FindElement.(option2)。那不是我需要的。我需要的东西,将找到一个元素的一个或其他定位

    • 好的,到目前为止,我已经把我的程序转到我想下载链接的网站并选择它,然后firefox对话框出现,我不知道该怎么办。我想将此文件保存到桌面上的文件夹中。我每晚都用这个,所以我需要它来工作。请帮忙。 以下是我从网站上获取下载链接的代码:

    • 问题内容: 我正在尝试搜寻房地产网站上的商品。它有一个aspx表单,必须在提交之前填写。 http://www.cbre.us/PropertyListings/Pages/Properties-for- Sale.aspx 我只关心俄勒冈州的多户家庭财产。所以这是我的第一次尝试: 当我运行此脚本时,出现错误“找不到元素“ ForSalePropertyType”。在这里我在做什么错? 问题答案: