gpt4 book ai didi

python - 使用程序化 Web 浏览时无法使用检查元素访问输入字段

转载 作者:行者123 更新时间:2023-12-04 16:18:56 25 4
gpt4 key购买 nike

当我使用 mechanize、selenium 库来运行一个 url(在这种情况下是“www.maps.google.com”),

Chrome 打开时会提示“Chrome 正在由自动化测试软件控制”。

但是当我尝试检查元素时,只有一个元素:body。所有其他输入和按钮均未显示。

我想自动化查找两个地址之间的距离的过程,那么如何解决上述问题?

from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:/chromedriver.exe')
# Go to your page url
driver.get('https://www.google.com/maps')
# Get button you are going to click by its id ( also you could us find_element_by_css_selector to get element by css selector)
button_element = driver.find_element_by_id('searchbox-directions')
button_element.click()
dest_add = driver.find_element_by_class_name("tactile-searchbox-input")
dest_add.send_keys("Agra")
start_add = driver.find_element_by_class_name("tactile-searchbox-input")
start_add.send_keys("Jaipur")

例如,这不起作用。因为,自然没有输入字段,也没有类名称为“tactile-searchbox-input”的元素。

最佳答案

诱导 WebDriverWait () 并等待 element_to_be_clickable () 和以下 css 选择器

driver.get('https://www.google.com/maps')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"searchbox-directions"))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[aria-label*='starting point']"))).send_keys("Agra")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[aria-label*='destination']"))).send_keys("Jaipur")

您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

点击搜索按钮试试这个。
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"(//button[@aria-label='Search'])[last()]"))).click()

关于python - 使用程序化 Web 浏览时无法使用检查元素访问输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61642463/

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