硒Webdriver没有这样的元素:无法找到元素

问题描述:

我是新的Python的硒webdriver学生。硒Webdriver没有这样的元素:无法找到元素

我想单击()下一个按钮,但它不起作用。

我在这里呆了几天。

谢谢你的帮助。

消息错误

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='next_button']"} 

这是我的代码

browser.find_element_by_xpath("//*[@id='next_button']").click() 

这是网页源

<div id=“banner" class="shell" style="width: 786px; background-color: rgb(0, 0, 255);"> 
<input id=“json" value=“x" type="hidden"> 
    <div class="shell"> 
     <div class="border"> 
      <div id="header" style="height: 135px;"> 
      <div id="navigation"> 
      <div id="main" style="background-color:#FFFFFF;"> 
       <div id="content" class="left" style="padding: 30px 0px 20px; left: 250px;"> 
        <style> 
        <div style="width: 100%;"> 
         <div class="inPn" style="width: 100%;background: #ffffff"> 
          <div class="detail"> 
           <h3>Example Event</h3> 
           <table class="fit"> 
            <colgroup> 
            <tbody> 
             <tr valign="middle"> 
              <td> 
              <td> 
               <div style="float:right; padding:5px;"> 
                <img id=“back_button" src="https://www.site/back.png" style="cursor: pointer;"> 
                <img id=“next_button" src="https://www.site/next.png" style="cursor: pointer;margin-top: 10px;" 
+0

你确定这是不是一个IFRAME里面,它是可见的?顺便说一句,不要使用XPath来做这种事情......使用'.find_element_by_id(“next_button”)'。 XPath速度更慢,更脆弱,并且浏览器之间的支持不一致。首选id然后选择CSS Selectors ...然后如果您需要通过包含文本的元素获取元素,请使用XPath。 – JeffC

当你的元素需要从src获得图像,您可能需要等待一段时间,直到出现页面上的目标元素:

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

button = wait(browser, 10).until(EC.presence_of_element_located((By.XPATH,"//img[@id='next_button']"))) 
button.click() 
+0

对不起,我的错误,现在我在src编辑。 – rurucatcat

+0

我做你的建议,这是行不通的,这个错误**'WebDriver'对象不可调用** – rurucatcat

+0

你可以显示导致此异常的行吗? – Andersson

我也建议等待页面使用下面的代码加载:

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

try: 
    element = WebDriverWait(browser, 15).until(EC.presence_of_element_located((By.XPATH, "//img[@id='next_button']"))) 
    browser.find_element_by_xpath("//img[@id='next_button']").click()  
except TimeoutException: 
    print 'Timeout - No tag found' 
    continue 

或使用:

WebDriverWait(browser, 15).until(lambda driver: driver.find_elements(By.XPATH,"//img[@id='next_button']")) 
+0

错误是**超时 - 找不到标签** 但我确定xpath是正确的,因为我使用firepath, – rurucatcat

+0

您使用哪个webdriver? –

+0

我使用mac上的chromedriver python2.7 – rurucatcat