如何从Python Selenium中的类提取所有href? [英] How to extract all href from a class in Python Selenium?
问题描述
我正在尝试从URL
我非常感谢您的帮助!
要从URL https://www.dx3canada.com/agenda/speakers ,因为所需元素位于< iframe>
中,因此您必须:
- 诱导 WebDriverWait 以使所需的框架可用并切换到.
- 促使 WebDriverWait 获取所位于所有元素的可见性.
-
您可以使用以下定位器策略:
从硒导入Webdriver的从selenium.webdriver.support.ui导入WebDriverWait来自selenium.webdriver.common.by导入方式从selenium.webdriver.support导入EC的预期条件选项= webdriver.ChromeOptions()options.add_argument(开始最大化")options.add_experimental_option("excludeSwitches",[启用自动化"])options.add_experimental_option('useAutomationExtension',False)driver = webdriver.Chrome(options = options,execute_path = r'C:\ WebDrivers \ chromedriver.exe')driver.get('https://www.dx3canada.com/agenda/speakers')WebDriverWait(driver,30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#whovaIframeSpeaker")))打印([[WebDriverWait中的my_elem的my_elem.get_attribute("href")(驱动程序,30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"a.display-flex.card.vancouver"))])))
-
控制台输出:
['https://whova.com/embedded/speaker_detail/dcrma_202003/9942778/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907682/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907688/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907676/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907696/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907690/'、'https://whova.com/embedded/speaker_detail/dcrma_202003/9907670/'、'https://whova.com/embedded/speaker_detail/dcrma_202003/9907693/','https://whova.com/embedded/speaker_detail/dcrma_202003/9942779/','https://whova.com/embedded/speaker_detail/dcrma_202003/9908087/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907671/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907681/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907673/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907678/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907689/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907674/'、'https://whova.com/embedded/speaker_detail/dcrma_202003/9907684/'、'https://whova.com/embedded/speaker_detail/dcrma_202003/9907685/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907686/','https://whova.com/embedded/speaker_detail/dcrma_202003/9942780/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907695/'、'https://whova.com/embedded/speaker_detail/dcrma_202003/9907687/'、'https://whova.com/embedded/Speaker_detail/dcrma_202003/9907683/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907692/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907672/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907697/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907680/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907679/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907675/','https://whova.com/embedded/speaker_detail/dcrma_202003/9907677/","https://whova.com/embedded/speaker_detail/dcrma_202003/9907694/"]
I am trying to extract people's href from the URL https://www.dx3canada.com/agenda/speakers.
I tried:
elems = driver.find_elements_by_css_selector('.display-flex card vancouver')
href_output = []
for ele in elems:
href_output.append(ele.get_attribute("href"))
print(href_output)
But the output list returns nothing...
The expected href shown as the image below and I hope the outputs as a list of hrefs:
I really appreciate the help!
To extract the people's href attribute from the URL https://www.dx3canada.com/agenda/speakers as the the desired elements are within an <iframe>
so you have to:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the visibility of all elements located.
You can use the following Locator Strategies:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe') driver.get('https://www.dx3canada.com/agenda/speakers') WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#whovaIframeSpeaker"))) print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.display-flex.card.vancouver")))])
Console Output:
['https://whova.com/embedded/speaker_detail/dcrma_202003/9942778/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907682/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907688/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907676/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907696/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907690/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907670/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907693/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9942779/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9908087/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907671/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907681/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907673/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907678/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907689/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907674/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907684/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907685/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907686/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9942780/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907695/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907687/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907683/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907692/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907672/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907697/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907680/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907679/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907675/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907677/', 'https://whova.com/embedded/speaker_detail/dcrma_202003/9907694/']
Here you can find a relevant discussion on Ways to deal with #document under iframe
这篇关于如何从Python Selenium中的类提取所有href?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!