【发布时间】:2017-12-20 08:06:53
【问题描述】:
我有一个简单的脚本,可以导航到下面以黄色和绿色突出显示的以下页面。
我的问题是,我如何创建一个页面计数器,以便我知道这项工作的位置?
理想情况下应该是:
Page 5
Page 4 #For each of the loop
尽管我遇到以下问题,但以下方法往往有效:
elements = [x.get_text("*") for x in
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
完整代码:
import collections
from random import shuffle
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
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
driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get('https://www.bet365.com.au/#/AS/B1/')
driver.get('https://www.bet365.com.au/#/AS/B1/')
def page_counter():
for x in range(1000):
yield x
clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()])')))
options = driver.find_elements_by_xpath('//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')
indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:
count = page_counter()
driver.get('https://www.bet365.com.au/#/AS/B1/')
elements = [x.get_text("*") for x in
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
#elements = [x.get_attribute("href") for x in
# driver.find_elements_by_xpath('//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
clickMe.click()
shuffle(elements)
links = dict((next(count) + 1, e) for e in elements)
desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))
for key, value in desc_links.items():
try:
driver.get(value)
print('Page ' + str(key))
except TimeoutException as ex:
pass
错误:
line 36, in <module>
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[div/div/text()="Main Lists"]//div[starts-with(@class, "sm-CouponLink_Label") and normalize-space()]')))]
File "C:\Users\Django\AppData\Local\Continuum\miniconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
【问题讨论】:
-
wait(driver, 10).until(EC.element_to_be_clickable(locator))打算返回不可迭代的 WebElement。试试wait(driver, 10).until(EC.visibility_of_all_elements_located(locator))。还要添加get_text()函数的定义 -
@Andersson 在这种情况下 get_text 会是什么样子,我在添加 css 时遇到了同样的错误
-
你问我
get_text()吗?我不知道它应该是什么样子 - 这是你的代码,你的功能:) ...当我添加 css... 添加到什么?更详细地描述问题并提供准确的错误日志 -
@Andersson 我已经发布了示例。这里:pastebin.com/fyXjkLi0/我通常只处理 HREF,所以我对如何为点击非 href 进行倒计时有点困惑。
标签: python python-3.x selenium