没有单击所有选项卡,也没有在出现问题时循环播放 [英] Not clicking all tabs and not looping once issues
问题描述
我正尝试点击网页上的标签,如下所示.不幸的是,尽管检查Chrome中的xpath正确无误,但它似乎似乎只单击了某些选项卡.我只能假设它没有点击所有标签,因为没有使用完整的xpath.
I am trying to click the tabs on the webpage as seen below. Unfortunately, it only seems to click some of the tabs despite correct correct xpath in inspect Chrome. I can only assume it’s not clicking all the tabs because the full xpath is not being used.
但是.. 我尝试过更改xpath:
However.. I have tried changing the 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:
However the issue persists. I have also tried using CSS:
#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--disabled > div > div.KambiBC-time-ordered-list-container > div.KambiBC-time-ordered-list-content > div > div > div.KambiBC-collapsible-container.KambiBC-mod-event-group-container > header
但是,这总是给我带来错误…… 对于:
However this keeps giving me errors… For:
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选择器来找到足够具体的元素,因为我认为在这种情况下不允许您缩小类元素的范围.
It should be noted that I want to click all the unopened tabs and I cannot seem to use CSS Selectors to find a specific enough element as I believe it does not allow you to narrow down the class element in this case.
是否可以解决没有单击所有内容的问题?
Is there a way to get around this issue of not clicking everything?
应注意,我正在使用...
It should be noted that I am using...
对于索引中的索引:
indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:
是否有一种用于1个循环的更优雅的方式?
Is there a more elegant way of using for 1 loop?
[import sys
sys.exit()][1]
完整的代码
推荐答案
这将逐个循环遍历每个联赛的所有比赛,并根据需要收集所有相关数据.您可以在每个匹配项中收集更多数据,方法是为每个查询加上.
作为前缀,然后通过match.find_element_by_xpath('.//your-query-here')
选择匹配项.让我知道这是否成功了!
This cycles through all the matches from each league 1 by 1, collecting all the relevant data as needed. You can collect further data within each match by prefixing each query with .
and selecting a match via match.find_element_by_xpath('.//your-query-here')
. Let me know if this did the trick!
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)
这篇关于没有单击所有选项卡,也没有在出现问题时循环播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!