Here is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
import time
N=22
while N<23:
driver = webdriver.Safari()
driver.get("https://www.totalcorner.com/league/view/1/page:22")
driver.maximize_window()
odds = {"Name":[],
"Over":[],
"Under":[]
}
done="No"
n=0
while done == "No":
n += 1
try:
check = driver.find_element_by_xpath("//tbody/tr[{}]".format(n))
except:
print("error")
done = "Yes"
break
check_if_date = driver.find_element_by_xpath("//tbody/tr[{}]/td[1]".format(n))
check_if_date = check_if_date.text
if '201' not in check_if_date:
button_odds = driver.find_element_by_xpath("//tbody/tr[{}]/td[15]/a[1]/button".format(n))
#open the new tab
button_odds.click()
time.sleep(5)
#here is the problem
driver.switch_to.window(driver.window_handles[-1])
print("switched")
time.sleep(5)
odds_name = driver.find_element_by_xpath("//body/div[2]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/table[3]/tbody[1]/tr[2]/td[1]")
odds_over = driver.find_element_by_xpath("//body/div[2]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/table[3]/tbody[1]/tr[2]/td[2]")
odds_under = driver.find_element_by_xpath("//body/div[2]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/table[3]/tbody[1]/tr[2]/td[3]")
name = odds_name.text
name = name.split('(')[1]
name = name.split(')')[0]
over = odds_over.text
over = over.split("@")[1]
under = odds_under.text
under = under.split("@")[1]
odds["Name"].append(name)
odds["Over"].append(over)
odds["Under"].append(under)
driver.close()
driver.switch_to.window(driver.window_handles[0])
N += 1
The first issue I encountered was that when opening the new tab selenium was still working on the first tab. To solve this I used
driver.switch_to.window(driver.window_handles[-1])
However this doesn't work either. The issue is that sometimes, it seems randomly, selenium won't switch tabs the first time.