当找不到信息时如何循环 'WebDriverWait' 函数?
How do I loop 'WebDriverWait' function when it cannot find an information?
如何重复(循环)下面的代码,即使 'URL' 案例不可用?
例如,我 excel 在 Discogs.com 上有许多 URL 的音乐。
https://www.discogs.com/master/561393-The-High-School-Musical-Cast-High-School-Musical-2-Soundtrack
https://www.discogs.com/master/1338438-Zedd-Maren-Morris-Grey-The-Middle
https://www.discogs.com/master/606957-Zendaya-Zendaya
...
但是,有些行没有我想要的确切信息。 (流派..)
目前,代码显示“NoSuchWindowException ...错误......”
我如何忽略不可用的 URL 并重复下一个循环?
driver.get(url)
genre = None
try:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
genre = [genre.text for genre in WebDriverWait(driver, 3).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr/th[@scope='row' and contains(., 'Genre')]//following::td[1]//a")))]
except selenium.common.exceptions.NoSuchElementException:
if genre != None:
song_url_dict_excel.iloc[i,2] = genre
要消除 None 类型错误,您可以应用 if else None
语句。
试试这个:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://www.discogs.com/master/561393-The-High-School-Musical-Cast-High-School-Musical-2-Soundtrack'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
genre = wait.until(EC.visibility_of_all_elements_located((By.XPATH, '(//*[@class="table_1fWaB"]/tbody/tr)[1]/td//a')))
for gen in genre:
gen = gen.text if gen else None
print(gen)
输出:
Electronic
Rock
Pop
Stage & Screen
如何重复(循环)下面的代码,即使 'URL' 案例不可用?
例如,我 excel 在 Discogs.com 上有许多 URL 的音乐。
https://www.discogs.com/master/561393-The-High-School-Musical-Cast-High-School-Musical-2-Soundtrack
https://www.discogs.com/master/1338438-Zedd-Maren-Morris-Grey-The-Middle
https://www.discogs.com/master/606957-Zendaya-Zendaya
...
但是,有些行没有我想要的确切信息。 (流派..) 目前,代码显示“NoSuchWindowException ...错误......” 我如何忽略不可用的 URL 并重复下一个循环?
driver.get(url)
genre = None
try:
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
genre = [genre.text for genre in WebDriverWait(driver, 3).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr/th[@scope='row' and contains(., 'Genre')]//following::td[1]//a")))]
except selenium.common.exceptions.NoSuchElementException:
if genre != None:
song_url_dict_excel.iloc[i,2] = genre
要消除 None 类型错误,您可以应用 if else None
语句。
试试这个:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
url = 'https://www.discogs.com/master/561393-The-High-School-Musical-Cast-High-School-Musical-2-Soundtrack'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
genre = wait.until(EC.visibility_of_all_elements_located((By.XPATH, '(//*[@class="table_1fWaB"]/tbody/tr)[1]/td//a')))
for gen in genre:
gen = gen.text if gen else None
print(gen)
输出:
Electronic
Rock
Pop
Stage & Screen