Python 为什么滚动和我的网页垃圾互动不好?

Python 为什么滚动和我的网页垃圾互动不好?,python,selenium,web-scraping,selenium-chromedriver,click,Python,Selenium,Web Scraping,Selenium Chromedriver,Click,我正试图在skybet上为给定的游戏争取所有的角逐赌注,但滚动似乎把我的循环搞乱了。当我打印section.text时,它看起来像是在做我想做的事情,但是它点击了错误的东西? 当我不滚动时,它只会在代码冻结之前单击前几个赔率部分。 非常感谢您的帮助,谢谢 此外,我让每个迭代中的部分刷新自己,因为我认为这可能是问题所在 driver = webdriver.Safari() driver.get("https://m.skybet.com/football/competitions&qu

我正试图在skybet上为给定的游戏争取所有的角逐赌注,但滚动似乎把我的循环搞乱了。当我打印section.text时,它看起来像是在做我想做的事情,但是它点击了错误的东西? 当我不滚动时,它只会在代码冻结之前单击前几个赔率部分。 非常感谢您的帮助,谢谢

此外,我让每个迭代中的部分刷新自己,因为我认为这可能是问题所在

driver = webdriver.Safari()
driver.get("https://m.skybet.com/football/competitions")
driver.maximize_window()

#click accept cookie
try:
    button_cookie = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "//body/div[2]/div[1]/a[2]"))
        )
    button_cookie.click()
except:
    print("no cookie")

#find location of premier league     
pl = driver.find_elements_by_class_name("split__title")
locate_pl=0
link_name = pl[locate_pl].text
while link_name != "Premier League":
    locate_pl += 1
    link_name = pl[locate_pl].text

pl[locate_pl].click()    
N = locate_pl + 1
#use N now to find pl matches
time.sleep(2)


#click on first match 
button_match = driver.find_element_by_xpath("//div[@id='competitions']/ul[1]/li[{}]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/a[1]".format(N))
teams = driver.find_element_by_xpath("//div[@id='competitions']/ul[1]/li[{}]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/a[1]/b/span".format(N))
button_match.send_keys(Keys.ENTER)  
time.sleep(2)

#find and click corners button
try:
    button_corners = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"_1ouz2ki")))
#button_corners = driver.find_elements_by_class_name("_1ouz2ki")
except:
    print("no corners")
n=0
link_name = button_corners[n].text
while link_name != "Corners":
    n += 1
    link_name = button_corners[n].text

button_corners[n].click()

#Now we will scrape all corner odds for this game.
odds_sections = driver.find_elements_by_class_name('_t0tx82')
N_sections = len(odds_sections)
c=0
scroll_to = 35
#the issue is within this loop
while c <= N_sections:
    
    odds_sections = driver.find_elements_by_class_name('_t0tx82')
    section = odds_sections[c]

    print(section.text)
    section.click()
    time.sleep(2)
    section.click()
    c += 1
    driver.execute_script("window.scrollTo(0,{})".format(scroll_to))
        
driver=webdriver.Safari()
驱动程序。获取(“https://m.skybet.com/football/competitions")
驱动程序。最大化_窗口()
#单击接受cookie
尝试:
button_cookie=WebDriverWait(驱动程序,20)。直到(
EC.element可点击((By.XPATH,//body/div[2]/div[1]/a[2]”)
)
按钮\u cookie。单击()
除:
打印(“无cookie”)
#查找英超联赛的位置
pl=驱动程序。按类名称(“拆分标题”)查找元素
定位\u pl=0
link\u name=pl[locate\u pl].text
而链接_name!=“英超联赛”:
定位_pl+=1
link\u name=pl[locate\u pl].text
pl[locate_pl]。单击()
N=定位\u pl+1
#现在使用N查找pl匹配项
时间。睡眠(2)
#单击第一个匹配项
button_match=driver。通过xpath(“//div[@id='competitions']/ul[1]/li[{}]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/a[1]”查找元素。格式(N))
teams=driver.通过xpath(“//div[@id='competitions']/ul[1]/li[{}]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/a[1]/b/span.格式(N))
按钮匹配。发送键(键。输入)
时间。睡眠(2)
#查找并单击“拐角”按钮
尝试:
button\u corners=WebDriverWait(驱动程序,10)。直到(所有元素的存在((按.CLASS\u名称,“\u 1ouz2ki”))
#button\u corners=驱动程序。通过类名称(“UZ2KI”)查找元素
除:
打印(“无角”)
n=0
链接\名称=按钮\角[n]。文本
而链接_name!=“角落”:
n+=1
链接\名称=按钮\角[n]。文本
按钮\u角[n]。单击()
#现在我们将为这场比赛赢得所有角球机会。
赔率部分=驱动程序。通过类名称(“t0tx82”)查找元素
N_段=len(赔率_段)
c=0
滚动至=35
#问题就在这个循环中

虽然我不能打开那个网站,因为它说它对我来说不存在,可能是因为国家限制,但我猜你的错误是每次迭代增加35个滚动,而不是仅仅滚动35个像素
scroll\u to+=35
应该是
scroll\u to=35
。但这并不能解决问题。已编辑代码。您可以在此处发布控制台的任何输出吗?DOM可能在获取赔率\u节数组之后或期间更新(可能在更新或更新.click()之后或在滚动之后…)尝试/捕获.text和.click()调用的异常以进行检查。如果是这种情况,您将看到“stale元素”异常。(再次抓取元素…)记住这是一个webelements数组,它是对DOM的引用的集合。这有点像一堆指针指向正在变化的内存。