单击周期中的链接时不可见元素

时间:2019-11-11 09:49:05

标签: python selenium selenium-webdriver

我从odds-co类中获得了所有链接。然后,在循环中,我click踩着每个。但是我有时会收到错误element not visible。我了解这是由于时间安排。在我看来,指定time.sleep ()并不是一个非常正确的方法。我认为 可以以某种方式使用webdriver wait,但我不知道如何使用。

rows = driver.find_elements_by_css_selector('.odds-co')
for i in rows:
    i.click()

url-https://www.oddsportal.com/soccer/england/efl-trophy/shrewsbury-macclesfield-WUgMbMnT/#over-under;2

2 个答案:

答案 0 :(得分:2)

expected_conditions.visibility_of()接收WebElement作为参数

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 10)
rows = driver.find_elements_by_css_selector('.odds-co')
for row in rows:
    wait.until(ec.visibility_of(row)).click()

您还可以等待所有行都出现

rows = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.table-container:not([style="display: none;"]) .odds-co')))
for row in rows:
    row.click()

答案 1 :(得分:2)

表中存在的行数为15,但是当您在DOM中搜索时,它显示为16。Over/Under 4.25 is missing.

得出WebDriverWaitpresence_of_all_elements_located()并检查12个元素并继续。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome()
driver.get("https://www.oddsportal.com/soccer/england/efl-trophy/shrewsbury-macclesfield-WUgMbMnT/#over-under;2")
rows=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".odds-co >a")))
for row in range(len(rows)):

    rows = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".odds-co >a")))
    if row==11:
        continue
    else:
        rows[row].click()

让我知道怎么回事。