使用Selenium和Python来谷歌Href " Getting this error NoSuchElementException: Message: no such element: Unab



使用selenium和python搜索href或单击链接打开它们,但出现此错误NoSuchElementException: Message: no such element: Unable to locate element:">

这是我写的代码。

url = "https://www.google.com/search?q="+text_fetch # The Dutch Dress in Orange—Why?
driver.get(url)
time.sleep(7)
Ggl_results = driver.find_element(By.XPATH, '//*[@id="rso"]/div[1]/div/div/div[1]/div/a')  # finds webresults
time.sleep(7)
for result in Ggl_results:
print(result.get_attribute("href"))

这个XPath应该工作,而不是'//*[@id="rso"]/div[1]/div/div/div[1]/div/a'//*[@id="rso"]//a[@data-ved][@ping]。我在其他随机输入上进行了测试,结果是有效的
此外,应该使用time.sleep(7)WebDriverWaitexpected_conditions显式等待,而不是硬编码睡眠
此编码有效:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.google.com/search?q=The Dutch Dress in Orange—Why?"
driver.get(url)
results = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='rso']//a[@data-ved][@ping]")))
for result in results:
print(result.get_attribute("href"))

这是输出:

https://www.dutchamsterdam.nl/321-why-the-dutch-wear-orange
https://netherlandsinsiders.com/why-is-the-national-color-of-the-netherlands-orange/
https://www.distractify.com/p/why-do-netherlands-wear-orange
https://dutchreview.com/culture/history/why-do-the-netherlands-love-orange-the-full-explainer/
https://www.quora.com/Why-do-the-Dutch-wear-orange-when-their-flag-doesnt-have-orange-in-it
https://aboutthenetherlands.com/the-reason-why-dutch-people-like-the-orange-color/
https://www.iamexpat.nl/lifestyle/lifestyle-news/most-googled-why-do-dutch-wear-orange
https://taiwanholland.com/holland-a-z/how-to-dress-up-orange-like-the-dutch/
https://amsterdamhangout.com/why-is-orange-the-national-colour-of-the-netherlands/

相关内容

  • 没有找到相关文章

最新更新