Not clicking all tabs and not looping once issues

2024/9/20 20:32:53

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. enter image description here

However.. I have tried changing the xpath:

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

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

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))))    

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))))

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...

for index in indexes:

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

Is there a more elegant way of using for 1 loop?

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

Full code

Answer

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 webdriverdriver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()try:os.remove('vtg121.csv')
except OSError:passdriver.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 Namexp_ln = './/span[@class="KambiBC-mod-event-group-header__main-title"]'ln = league.find_element_by_xpath(xp_ln).text.strip()except:ln = Noneprint(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).textexcept:team1_name = Nonetry:team1_odds = match.find_element_by_xpath(xp_team1_odds).textexcept:team1_odds = Nonetry:team2_name = match.find_element_by_xpath(xp_team2_name).textexcept:team2_name = Nonetry:team2_odds = match.find_element_by_xpath(xp_team2_odds).textexcept:team2_odds = Nonedata.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 = Noneprint(ln)for match in matches:try:team1_name = match.find_element_by_xpath(xp_team1_name).textexcept:team1_name = Nonetry:team1_odds = match.find_element_by_xpath(xp_team1_odds).textexcept:team1_odds = Nonetry:team2_name = match.find_element_by_xpath(xp_team2_name).textexcept:team2_name = Nonetry:team2_odds = match.find_element_by_xpath(xp_team2_odds).textexcept:team2_odds = Nonedata.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)
https://en.xdnf.cn/q/72132.html

Related Q&A

Opencv stream from a camera connected to a remote machine

I am developing a wx application in python for streaming and displaying video from two different webcams. This works fine, but now I need to do this in a different scenario in which the two cameras are…

Is there a callable equivalent to f-string syntax?

Everybody loves Python 3.6s new f-strings:In [33]: foo = {blah: bang}In [34]: bar = blahIn [35]: f{foo[bar]} Out[35]: bangHowever, while functionally very similar, they dont have the exact same semanti…

Python: list comprehension based on previous value? [duplicate]

This question already has answers here:Python list comprehension - access last created element(9 answers)Closed 10 months ago.Say I want to create a list using list comprehension like:l = [100., 50., 2…

How to run a coroutine inside a context?

In the Python docs about Context Vars a Context::run method is described to enable executing a callable inside a context so changes that the callable perform to the context are contained inside the cop…

Random Forest interpretation in scikit-learn

I am using scikit-learns Random Forest Regressor to fit a random forest regressor on a dataset. Is it possible to interpret the output in a format where I can then implement the model fit without using…

Why are three apostrophes needed for print in Python?

Im making this Pythagoras Theorem Calculator in Python 3.3.2.I made print over several lines so that I could make a diagram:print("Welcome to the Pythagoras Theorem Calculator, powered by Python!&…

Downloading files from public Google Drive in python: scoping issues?

Using my answer to my question on how to download files from a public Google drive I managed in the past to download images using their IDs from a python script and Google API v3 from a public drive us…

Change locale for django-admin-tools

In my settings.py file I have:LANGUAGE_CODE = ru-RUalso, I have installed and working django-admin-tools. But admin language still english. What Im doing wrong?PS.$ cat settings.py | grep USE | grep -…

Container localhost does not exist error when using Keras + Flask Blueprints

I am trying to serve a machine learning model via an API using Flasks Blueprints, here is my flask __init__.py filefrom flask import Flaskdef create_app(test_config=None):app = Flask(__name__)@app.rout…

Serving static files with WSGI and Python 3

What is the simplest way to serve static files with WSGI and Python 3.2? There are some WSGI apps for PEP 333 and Python 2 for this purpose - but was is about PEP 3333 and Python 3? I want to use wsg…