Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 3.x 在Selenium Python中找不到元素_Python 3.x_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 3.x 在Selenium Python中找不到元素

Python 3.x 在Selenium Python中找不到元素,python-3.x,selenium,xpath,css-selectors,webdriverwait,Python 3.x,Selenium,Xpath,Css Selectors,Webdriverwait,您好,我正在尝试使用selenium查找要单击的按钮。下面是我正在使用的HTML代码片段 <input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()"> 我收到以下错误消息: NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]

您好,我正在尝试使用selenium查找要单击的按钮。下面是我正在使用的HTML代码片段

<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
我收到以下错误消息:

NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
不知道还有什么可以尝试的


查找元素最可能需要的是使用等待。在与元素交互之前,您需要留出时间让元素可见、可单击等。您可以在此处找到有关等待的信息:

摘自上述网站:

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

elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
如果等待不起作用,那么元素可能在iframe中。您需要首先切换到该iframe,然后搜索元素以找到它

您将找到iframe,就像找到另一个元素一样,然后像这样切换到它:

iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)

button = driver.find_element_by_id("runButton")
button.click()
完成iframe及其内容后,需要切换回iframe:

driver.switch_to.default_content()

元素似乎是一个动态元素,因此要在元素上单击(),您需要使用
element\u to\u be\u clickable()
,并且可以使用以下任一选项:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button#runButton[value='Run Report']"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='button' and @id='runButton'][@value='Run Report']"))).click()
    
  • 注意:您必须添加以下导入:

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

请附上inspect element页面的屏幕截图以及您迄今为止尝试过的代码。帮助开发人员找到问题的解决方案也是如此。主要是调试。如果不提供代码,就很难帮助您:)我发布了inspect元素的HTML以及我尝试的代码。我接着附上了一张检查元件的照片。谢谢你的回复。我试过了,甚至把等待时间设为30分钟,结果都没用。我甚至找不到父元素,但也找不到它的ID。可能是因为selenium中没有任何可见的内容或存在bug吗?如果您添加了waits,但它仍然没有找到元素,那么它可能位于iFrame中。你能把页面的html添加到你的问题中吗?我想我的问题是我在这个页面上找不到任何东西。我尝试使用element by Id方法查找元素,但找不到Id。按钮似乎位于表中。我已经发布了上面HTML的屏幕截图。即使它在一个表中,您也应该能够找到它。你能给出所有的html吗?如果您的按钮位于iframe内,则在首先切换到该iframe之前,您将无法检测。我可以确定此表位于iframe内。请参阅下面的Iframe。我无法发布整个HTML代码。我可以通过它的ID找到这个iframe。接下来我需要做什么?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC