1

我是新的 python selenium 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;"
4

2 回答 2

0

我还建议使用以下代码等待页面加载:

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']"))
于 2016-12-16T12:33:31.390 回答
0

由于您的元素需要从中获取图像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()
于 2016-12-16T11:40:25.753 回答