【发布时间】:2022-01-25 20:11:10
【问题描述】:
以下工作代码显示我能够从 Web 元素中检索文本,但不能从 href 中检索文本(返回无)。我究竟做错了什么?没有按预期工作的代码行是倒数第二行:
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
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(
"/Users/bob/Documents/work/AIFA/scraper/scrape_gu/chromedriver"
)
wait = WebDriverWait(driver, 30)
driver.get("https://farmaci.agenziafarmaco.gov.it/bancadatifarmaci/cerca-farmaco")
readunderstood = driver.find_element_by_id("conf")
readunderstood.click()
accept = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div[3]/div/button"))
)
accept.click()
# end of the initial agreement screens and general preparation
##############################################################
SEARCH_STRING = "AB" # we can safely assume this does not exist
find_textbox = driver.find_element_by_id("search")
find_textbox.clear() # after the first search the old value will still be there
find_textbox.send_keys(SEARCH_STRING)
find_textbox.send_keys(Keys.ENTER)
# end of the search for a drug action
##############################################################
drugs_list = wait.until(
EC.presence_of_all_elements_located(
(By.XPATH, "//*[@id='ul_farm_results']/li[@style='display: list-item;']",)
)
)
###### this is the part I don't understand
for drug in drugs_list:
print(drug.get_attribute("href")) # this should return a link, but returns None
print(drug.text) # this correctly prints 3 lines per drug
【问题讨论】:
标签: python selenium web-scraping xpath