Tet*_*ora 6 css python selenium xpath selenium-webdriver
我想点击网页上的标签,如下所示.不幸的是,它似乎只是点击一些选项卡,尽管在检查Chrome时正确的xpath是正确的.我只能假设它没有单击所有选项卡,因为没有使用完整的xpath.

但是..我试过改变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))))
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在.我也尝试过使用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
Run Code Online (Sandbox Code Playgroud)
然而,这一直给我错误...对于:
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))))
Run Code Online (Sandbox Code Playgroud)
应该注意的是,我想单击所有未打开的选项卡,我似乎无法使用CSS选择器来查找足够的特定元素,因为我认为它不允许在这种情况下缩小类元素.
有没有办法解决这个不点击一切的问题?
应该指出的是,我正在使用......
索引中的索引:
indexes = [index for index in range(len(options))]
shuffle(indexes)
for index in indexes:
Run Code Online (Sandbox Code Playgroud)
是否有更优雅的方式用于1循环?
[import sys
sys.exit()][1]
Run Code Online (Sandbox Code Playgroud)
完整代码
这会一一循环每个联赛的所有比赛,根据需要收集所有相关数据。您可以通过为每个查询添加前缀并通过.选择匹配来收集每个匹配中的更多数据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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
391 次 |
| 最近记录: |