如何提取随机生成的 id 值?

问题描述 投票:0回答:1

https://www.spinxo.com/

我正在尝试从该站点提取随机生成的 ID 值。 但即使我组合这些

find_element
选项,我也无法提取这些 ID 值。 我应该使用什么选项来提取它?

<ul class="clear list01"><li><a href="#" name="lnkName">2cutecher</a></li><li><a href="#" name="lnkName">Aceog</a></li><li><a href="#" name="lnkName">Bushba</a></li><li><a href="#" name="lnkName">Catcheman</a></li><li><a href="#" name="lnkName">Contencewo</a></li><li><a href="#" name="lnkName">Cozyfoot</a></li><li><a href="#" name="lnkName">Desigoesi</a></li><li><a href="#" name="lnkName">FlashTa</a></li><li><a href="#" name="lnkName">Gamyopra</a></li><li><a href="#" name="lnkName">Grimazon</a></li><li><a href="#" name="lnkName">Idolin</a></li><li><a href="#" name="lnkName">Inetenli</a></li><li><a href="#" name="lnkName">Jiggyny</a></li><li><a href="#" name="lnkName">Kenjill</a></li><li><a href="#" name="lnkName">LuvFlirty</a></li><li><a href="#" name="lnkName">Metabre</a></li><li><a href="#" name="lnkName">MuraPira</a></li><li><a href="#" name="lnkName">Patepoi</a></li><li><a href="#" name="lnkName">Penhama</a></li><li><a href="#" name="lnkName">PlusAnguris</a></li><li><a href="#" name="lnkName">Rawcsto</a></li><li><a href="#" name="lnkName">Runen</a></li><li><a href="#" name="lnkName">SarenApril</a></li><li><a href="#" name="lnkName">Sexylo</a></li><li><a href="#" name="lnkName">Slashoest</a></li><li><a href="#" name="lnkName">ToughDressy</a></li><li><a href="#" name="lnkName">Tricar</a></li><li><a href="#" name="lnkName">Upforia</a></li><li><a href="#" name="lnkName">Uppertska</a></li><li><a href="#" name="lnkName">Wowro</a></li></ul>

enter image description here

driver.get('https://www.spinxo.com/')

spin_btn = driver.find_element(By.CLASS_NAME, 'spin')
spin_btn.click()

id_list = (driver.find_elements(By.NAME, 'lnkName'))

for id in range(len(id_list)):
    print(id_list[id].text)
python selenium-webdriver selenium-chromedriver
1个回答
0
投票

有两件事:

  1. 在单击 Spin 按钮
  2. 之前,您需要删除同意 cookie 弹出窗口
  3. 使用Selenium的Waits来有效地等待selenium定位到元素

请参考以下代码:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.spinxo.com/')
driver.maximize_window()
driver.implicitly_wait(10)

# Below line will click on Consent button - Use this line if you get the Consent pop-up
driver.find_element(By.XPATH, "//p[text()='Consent']").click()

spin_btn = driver.find_element(By.CLASS_NAME, 'spin')
spin_btn.click()

id_list = driver.find_elements(By.NAME, 'lnkName')

for id in id_list:
    print(id.text)

控制台输出:

2freethay
BabyZero
BagoYounger
Bellit
BraceLasting
CheerupSuru
CountryBlacken
Eirenga
Entelle
Heerer
Inedcity
Jungshoo
Katia
MelConfident
Mixedia
NumAmerica
Offshowan
Presbusi
PressMissing
Prodigyro
Selfcher
Skribitne
StroonsSunny
Strozvige
Tanyco
Tendreach
Tioniary
VamBe
WardZin
Xyloftwa

Process finished with exit code 0
© www.soinside.com 2019 - 2024. All rights reserved.