我有一個簡單的問題示例。我運行 driver.find_elements(By.ID, "thumbnail") 它有效,我單擊一個隨機元素,然后在回圈中再次重新抓取資訊,第二次,我總是得到完全相同的結果:
driver.get("https://www.somepage.com")
time.sleep(7)
items = []
for i in range(3):
print("LOOP #: " str(i))
random_number = random.randint(1, 5)
items = driver.find_elements(By.ID, "thumbnail")
url = i.get_attribute("href")
print(str(url))
items[random_number].click()
time.sleep(100)
輸出
LOOP #: 0
URL 1
URL 2
URL 3
URL 4
LOOP #: 1
URL 1
URL 2
URL 3
URL 4
LOOP #: 2
URL 1
URL 2
URL 3
URL 4
第二個回圈應該有不同的 URL。仍然find_elements(By.ID, "thumbnail")適用
我不知道我做錯了什么。我什至嘗試items.clear()在回圈結束時添加,結果相同。
uj5u.com熱心網友回復:
以下答案適用于,YouTube因為它是在被問到時作為示例給出的。打開時YouTube,它會有thumbnailid,并且這些縮略圖有很多集合。所以策略是在 3 的范圍內進行迭代,在該回圈中,對于每次迭代,收集所有具有 id 的元素thumbnail并選擇一個隨機元素并獲取它href,然后單擊它。現在的問題是如何重申:有 2 個選項:(1)繼續單擊并thumbnail從左窗格中選擇一個選項(我想),或者,(2)單擊主頁(YouTube圖示)然后再次繼續迭代程序。
我選擇了第二個選項,這里是它的代碼:
driver.get('https://www.youtube.com/')
for i in range(3):
print("LOOP #: " str(i))
time.sleep(10)
items = driver.find_elements(By.ID, "thumbnail")
# here, instead of selecting from the items, you are trying to fetch the attribute from i, which is not an element at all and it didn't work for me.
# I , instead, fetched the href from items stored it in a variable, and clicked on it, then clicked on homepage and reiterated the process
rand = random.choice(items)
print(rand.get_attribute('href'))
rand.click()
time.sleep(3)
driver.find_element(By.XPATH, "(//*[@title='YouTube Home'])[1]").click()
driver.quit()
輸出:
LOOP #: 0
https://www.youtube.com/watch?v=YIKz49-aGas
LOOP #: 1
https://www.youtube.com/watch?v=51Qs0Ej2RUc
LOOP #: 2
https://www.youtube.com/watch?v=OeShsZPOP-s
Process finished with exit code 0
注意:如果你想要一個健壯的代碼,你可以用更好的time.sleep替換。話雖如此,YouTube 作為 Google 的財產,在元素屬性中會有很大的隨機性,并且經常變得不穩定。此外,如果請求過多,機器人也會被檢測到。explicit waitwebdriverwait
更新答案:
更新了單擊首頁縮略圖后單擊右窗格縮略圖的答案
driver.maximize_window()
driver.get('https://www.youtube.com/')
time.sleep(10)
items = driver.find_elements(By.ID, "thumbnail")
rand = random.choice(items)
print(rand.get_attribute('href'))
rand.click()
time.sleep(3)
for i in range(3):
print(f"Loop#: {str(i)}")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "movie_player")))
yt_left_pane_items = driver.find_elements(By.XPATH, "//*[@id='items']//*[@id='thumbnail']")
rand_left_pane = random.choice(yt_left_pane_items)
print(rand_left_pane.get_attribute('href'))
rand_left_pane.click()
time.sleep(5)
driver.quit()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
輸出:
https://www.youtube.com/watch?v=9YSbflKeOZQ
Loop#: 0
https://www.youtube.com/watch?v=ENOEgKeI_D0
Loop#: 1
https://www.youtube.com/watch?v=PeByUAhHXqs
Loop#: 2
https://www.youtube.com/watch?v=GUHfY84weMw
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433133.html
標籤:Python python-3.x 硒 硒网络驱动程序
