基于@InfernO的XPath,这里有一个snip,它获取所有URL并单击第一个:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
# options.add_argument("--headless")
options.add_argument("--incognito")
searchList = ["pdf example", "pdf file"]
urls = []
for i, word in enumerate(searchList):
driver = webdriver.Chrome("C:\workspace\TalSolutionQA\general_func_class\chromedriver.exe", chrome_options=options)
driver.get("https://google.com/search?query=" + searchList[i])
all_urls = driver.find_elements_by_xpath("//a[contains(@href, '.pdf')]")
urls.append([i.get_attribute("href") for i in all_urls])
print(f'the urls:{[i.get_attribute("href") for i in all_urls]}')
all_urls[0].click()
driver.quit()
print(urls)
欢迎来到硒很多乐趣等着你!