我是新的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;"
答案 0 :(得分: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()
答案 1 :(得分: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']"))