不点击所有标签,不循环一次问题

2024-04-19 12:53:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试按一下网页上的标签,如下所示。不幸的是,尽管inspectchrome中正确的xpath,它似乎只点击了一些选项卡。我只能假设它没有点击所有的标签,因为没有使用完整的xpath。 enter image description here

但是。。 我尝试更改xpath:

//div[@class="KambiBC-collapsible-container KambiBC-mod-event-group-container"] 收件人:

//div[@class='KambiBC-event-groups-list']//div[@class="KambiBC-collapsible-container KambiBC-mod-event-group-container"] 用于:

clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'(//div[@class="KambiBC-collapsible-container KambiBC-mod-event-group-container"])[%s]' % str(index + 1))))    

但问题依然存在。 我也尝试过使用CSS:

^{pr2}$

但是这总是给我错误… 用于:

clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'("#KambiBC-contentWrapper__bottom > div > div > div > div > div.KambiBC-quick-browse-container.KambiBC-quick-browse-container--list-only-mode > div.KambiBC-quick-browse__list.KambiBC-delay-scroll > div > div.KambiBC-time-ordered-list-container > div.KambiBC-time-ordered-list-content > div > div > div > header")[%s]' % str(index + 1))))

需要注意的是,我想点击所有未打开的选项卡,我似乎不能使用CSS选择器来找到足够具体的元素,因为我认为在这种情况下,它不允许您缩小class元素的范围。在

有没有办法绕过这个问题,不点击所有的东西?在

需要注意的是,我正在使用。。。在

对于索引中的索引:

indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:

有没有更优雅的方式来使用for 1循环?在

[import sys
sys.exit()][1]

完整code


Tags: diveventmodindexcontainergroupquickcss
2条回答

这将循环从每个联盟1到1的所有比赛,收集所有需要的相关数据。通过在每个查询前面加上.并通过match.find_element_by_xpath('.//your-query-here')选择一个匹配项,可以在每个匹配中收集进一步的数据。让我知道这是否奏效!在

import sys, io, os, csv, requests, time
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium import webdriver

driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

try:
    os.remove('vtg121.csv')
except OSError:
    pass

driver.get('https://www.unibet.com.au/betting#filter/football')
time.sleep(1)

clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, 
    ('//div[@class="KambiBC-collapsible-container '\
    'KambiBC-mod-event-group-container"]'))))
time.sleep(0)

xp_opened = '//div[contains(@class, "KambiBC-expanded")]'
xp_unopened = '//div[@class="KambiBC-collapsible-container ' \
    'KambiBC-mod-event-group-container" ' \
    'and not(contains(@class, "KambiBC-expanded"))]'
opened = driver.find_elements_by_xpath(xp_opened)
unopened = driver.find_elements_by_xpath(xp_unopened)

data = []
for league in opened:
    xp_matches = './/li[contains(@class,"KambiBC-event-item")]'
    matches = league.find_elements_by_xpath(xp_matches)

    try:
        # League Name
        xp_ln = './/span[@class="KambiBC-mod-event-group-header__main-title"]'
        ln = league.find_element_by_xpath(xp_ln).text.strip()
    except:
        ln = None
    print(ln)

    for match in matches:
        # get all the data per 'match group'
        xp_team1_name = './/button[@class="KambiBC-mod-outcome"][1]//' \
            'span[@class="KambiBC-mod-outcome__label"]'
        xp_team1_odds = './/button[@class="KambiBC-mod-outcome"][1]//' \
            'span[@class="KambiBC-mod-outcome__odds"]'
        xp_team2_name = './/button[@class="KambiBC-mod-outcome"][3]//' \
            'span[@class="KambiBC-mod-outcome__label"]'
        xp_team2_odds = './/button[@class="KambiBC-mod-outcome"][3]//' \
            'span[@class="KambiBC-mod-outcome__odds"]'

        try:
            team1_name = match.find_element_by_xpath(xp_team1_name).text
        except:
            team1_name = None

        try:
            team1_odds = match.find_element_by_xpath(xp_team1_odds).text
        except:
            team1_odds = None

        try:
            team2_name = match.find_element_by_xpath(xp_team2_name).text
        except:
            team2_name = None

        try:
            team2_odds = match.find_element_by_xpath(xp_team2_odds).text
        except:
            team2_odds = None

        data.append([ln, team1_name, team1_odds, team2_name, team2_odds])

for league in unopened:
    league.click()
    time.sleep(0.5)
    matches = league.find_elements_by_xpath(xp_matches)

    try:
        ln = league.find_element_by_xpath(xp_ln).text.strip()
    except:
        ln = None
    print(ln)

    for match in matches:
        try:
            team1_name = match.find_element_by_xpath(xp_team1_name).text
        except:
            team1_name = None

        try:
            team1_odds = match.find_element_by_xpath(xp_team1_odds).text
        except:
            team1_odds = None

        try:
            team2_name = match.find_element_by_xpath(xp_team2_name).text
        except:
            team2_name = None

        try:
            team2_odds = match.find_element_by_xpath(xp_team2_odds).text
        except:
            team2_odds = None

        data.append([ln, team1_name, team1_odds, team2_name, team2_odds])

with open('vtg121.csv', 'a', newline='', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    for row in data:
        writer.writerow(row)
        print(row)

OP's code without extra imports

发生此错误是因为site对操作所需的制表符的XPath不连续。它有一个缺口。例如,现在我找不到

//*[@id=“KambiBC-contentWrapper_ubottom”]/div/div/div/div[3]/div1/div/div[3]/div[2]/div/div[2]/header

前一段时间在游戏开始前,我找不到

//*[@id=“KambiBC-contentWrapper_ubottom”]/div/div/div/div[3]/div1/div/div[3]/div[2]/div/div[1]/header

当我谈到index时,我指的是上面粗体部分。

当游戏上线时,标签会突然将索引从2变为1。(粗体部分更改)在这两种情况下,都存在间隙:要么找不到1,要么找不到2。在

我想,产生这种差距的原因是,两者之间还有一个元素是不可点击的。见下图。 enter image description here

这个league是造成这种差距的原因。因此,每当代码命中league所占的索引时,它就会超时。因为League按钮和其他选项卡会切换League的位置,所以当位置发生变化时,索引会交换。(我想这就是为什么我首先找不到粗体部分为1的Xpath,后来又找不到粗体部分为2的原因。)

下面是OP的一部分代码,你可以在末尾看到str(index+1)。在

indexes = [index for index in range(len(options))] # 
shuffle(indexes) # the OP use shuffle from random. Still 0 and 1 is contained.
path = '(//div[@class="KambiBC-collapsible-container KambiBC-mod-event-group-container"])'
for index in indexes:
    # Because there are some indexes are missing because of League button,
    # nothing can be found at the index and it times out.
    clickMe = wait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, path + '[%s]' % str(index + 1))))

解决方案

尝试捕获超时异常以跳过League所占用的索引。您还可以保留一个计数器,以便在一个页面上只捕获一个超时异常。如果有第二次超时,您知道除了League按钮之外还有其他问题,应该停止。在

^{pr2}$

相关问题 更多 >