0

I have the following CSS_SELECTOR which appears between 2 - 20 times on my page and the number changes everyday:

event = driver.find_elements(By.CSS_SELECTOR, ".sport-tennis .event-list .event-column-main")
random = event[randint(0, len(event)-1)]
random.click()

When click on it it takes me to another page which url changes everyday so it is not fixed url. Now as you see I did it by clicking randomly on that css_selector but my question is: is there a way to make a loop which clicks on all of the mentioned above css_selectors without repeating?

EDIT: Tried this but only clicks on the first link with that css_selector and opens the page but then when goes back to the main page doesn't click the second link and just finishes with exit code 0:

events = driver.find_elements(By.CSS_SELECTOR, ".sport-tennis .event-list .event-column-main")

for event in events:
    event.click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".sport-tennis .flex-column .text-truncate").click()
    time.sleep(1)
1
  • To solve this issue we normally get the total number of times we need to loop. In the enumerate loop we get the elements and click on the index of the element[i]. Commented Sep 24, 2020 at 10:29

2 Answers 2

1

I doubt that the element state might have changed on the each iteration, so try this,

for i in range(len(events)):
    events = driver.find_elements(By.CSS_SELECTOR, ".sport-tennis .event-list .event-column-main")
    event[i].click()
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".sport-tennis .flex-column .text-truncate").click()
    time.sleep(1)
Sign up to request clarification or add additional context in comments.

Comments

1

Let's call your list events instead of event, event variable will use it later, so your code can be

events = driver.find_elements(By.CSS_SELECTOR, ".sport-tennis .event-list .event-column-main")
for event in events:
    event.click();

3 Comments

Thanks for the help but strangely it clicks on the first link of that css_selector and opens the new page but then when it goes back the the previous page doesn't click on the second link with that css_selector but just finishes with exit code 0.
oh, ic - let me update the code, can you paste the page url?
Unfortunately it is not online so you won't be able to open it but I can give you similar page: bet365.com/#/IP/B13 and similar css_selector as mine .ovm-Fixture_Container .ovm-FixtureDetailsWithIndicators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.