Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';我在selenium中找不到带有占位符的输入元素?_Python_Selenium - Fatal编程技术网

Python 为什么可以';我在selenium中找不到带有占位符的输入元素?

Python 为什么可以';我在selenium中找不到带有占位符的输入元素?,python,selenium,Python,Selenium,在网页中,我有以下元素: <input placeholder="Search in your collabs" class="md-input" type="text"> 但在运行代码时,我得到以下错误: NoSuchElementException: Message: Unable to locate element: //input[@placeholder='Search in your collabs'] 我要查找的元素位于我之前正确选择的iframe中(之前我使用一些

在网页中,我有以下元素:

<input placeholder="Search in your collabs" class="md-input" type="text">
但在运行代码时,我得到以下错误:

NoSuchElementException: Message: Unable to locate element: //input[@placeholder='Search in your collabs']
我要查找的元素位于我之前正确选择的iframe中(之前我使用一些其他属性执行了另一个
find\u element\u by_xpath
)。具体来说,我以前做的是:

browser.switch_to_frame(browser.find_element_by_id("clb-iframe-workspace"));
那么,为什么现在不起作用呢?

使用
sleep
(如您的评论中所述)是一个糟糕的解决方案,因为它不可避免地会减慢测试的执行速度,并且可能每天都会失败。更好的解决方案是使用超时。超时等待到指定的时间跨度,但会尽快继续(即满足您的预期条件)

您应该增加查找元素的隐式等待超时,或者使用显式等待,例如

from selenium.webdriver.support import expected_conditions as expect
....
//wait up to 120sec, poll every sec
elem = WebDriverWait(browser, 120, 1).until(
        expect.visibility_of_element_located(
        (By.XPATH, "//input[@placeholder='Search in your collabs']")))

如前所述

共享如何切换到
iframe
请参阅我用于切换到
iframe
NoSuchElementException:Message:无法找到元素://input[@placeholder='Search in your collabs']
xpath切换框架的问题绝对没有问题。检查框架定位器“框架定位器”是什么意思?
from selenium.webdriver.support import expected_conditions as expect
....
//wait up to 120sec, poll every sec
elem = WebDriverWait(browser, 120, 1).until(
        expect.visibility_of_element_located(
        (By.XPATH, "//input[@placeholder='Search in your collabs']")))