【问题标题】:Page counter for css elementscss 元素的页面计数器
【发布时间】: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


【解决方案1】:

由于主列表下的列表不是 hrefs,您不能让驱动程序使用 driver.get() 打开所有列表(至少据我所知),您必须一一单击所有列表并获取信息。

看看这段代码是否有助于开始,用更好的等待逻辑替换 time.sleep

driver.get('https://www.bet365.com.au/#/AS/B1/')

driver.find_element_by_id('TopPromotionBetNow').click()

time.sleep(10)
classifications = driver.find_elements_by_class_name('wn-Classification')
for classification in classifications:
    if classification.text == 'Soccer':
        classification.click()
        break

time.sleep(10)
markets = driver.find_elements_by_class_name('sm-Market')
for market in markets:
    group_name = market.find_element_by_class_name('sm-Market_GroupName')
    if group_name.text == 'Main Lists':
        coupon_lables = [x for x in market.find_elements_by_class_name('sm-
                         CouponLink_Label')]
    break

for label in coupon_lables:
    print('executing:' + label.text)
    label.click()

    # do your stuff 
    # find the back button (<) on the web page, click on it
    # go back to select the next label

更新

coupon_lables = [x.text for x in market.find_elements_by_class_name('sm-CouponLink_Label')]

for label in coupon_lables:
    time.sleep(5)
    driver.find_element_by_xpath(f'//div[contains(text(), "' + label + '")]').click()
    time.sleep(5)
    driver.find_element_by_class_name('cl-BreadcrumbTrail_BackButton').click()

【讨论】:

  • 我收到:消息:过时的元素引用:元素未附加到页面文档 - pastebin.com/LHna0MCr
  • 哪一行抛出该错误?我无法复制它在我的系统上工作
  • 你能更新它吗:wait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ("wn-Classification")))) 或等效的。 time.sleep 似乎不起作用。
  • 更改为 By.CLASS_NAME,定位器是类名,它对我有用
  • 我已经更新了代码。我相信当您离开并返回单击它们时,主列表下的所有元素都会在 dom 中被重写。因此,我没有将元素存储在 coupon_lables 中,而是存储了文本,并且在浏览所有标签时,我使用文本来定位元素。这样,代码将搜索一个元素,而不是尝试使用存储的元素。将 time.sleep 替换为您的等待。话虽如此,如果主列表中的任何文本在不同类别下匹配,这可能不是一个好的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多