gpt4 book ai didi

python - 无法点击不同结果从内页解析标题

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:05 25 4
gpt4 key购买 nike

我用 python 编写了一个脚本,用于在谷歌地图的搜索框中启动搜索后填充结果。我的脚本完美地做到了这一点。现在,我试图点击 each of the resultsone layer deep并从那里解析标题。

When I run the script, I get one title successfully but then the script throws the same common error element is not attached to the dom, although I've taken all the measures to get rid of that.

website address

我在这里使用这个关键字 motels in new jersey 作为搜索。

我试过:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.google.com/maps/search/")
wait = WebDriverWait(driver, 10)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#searchboxinput"))).send_keys("motels in new jersey")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#searchbox-searchbutton"))).click()

while True:
try:
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class='section-result'] h3[class='section-result-title']"))):
# click on each of the results
item.click()
# the script now in inner page to parse the title
name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h1.section-hero-header-title-title"))).text
print(name)
# click on the "Back to results" link located on the top left to get back to the results page
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"button[class^='section-back-to-list-button']"))).click()
# wait for the spinner to be invisible
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#searchbox[class*='loading']")))
# tried to get rid of staleness condition
wait.until(EC.staleness_of(item))
except Exception:
break

如何点击不同的结果来解析内页的标题?

最佳答案

要继续遍历受 DOM 任何操作影响的列表,您必须在每个 .click() 事件后刷新元素列表,或任何需要在当前范围内加载元素的事件。请尝试以下代码来解决您的问题:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://www.google.com/maps/search/")
wait = WebDriverWait(driver, 10)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#searchboxinput"))).send_keys("motels in new jersey")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#searchbox-searchbutton"))).click()

while True:
try:
for count, item in enumerate(wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class='section-result'] h3[class='section-result-title']")))):
refreshList = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "[class='section-result'] h3[class='section-result-title']")))
# click on each of the results
refreshList[count].click()
# the script now in inner page to parse the title
name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"h1.section-hero-header-title-title"))).text
print(name)
# click on the "Back to results" link located on the top left to get back to the results page
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"button[class^='section-back-to-list-button']"))).click()
# wait for the spinner to be invisible
wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#searchbox[class*='loading']")))
# tried to get rid of staleness condition
wait.until(EC.staleness_of(refreshList[count]))
except Exception:
break

关于python - 无法点击不同结果从内页解析标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57140172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com