ChatGPT对话:Python程序自动模拟操作网页,无法弹出下拉列表框

【编者按】需要编写Python程序自动模拟操作网页。编者有编程经验,但没有前端编程经验,完全不知道如何编写这种程序。通过与ChatGPT讨论,1天完成了任务。因为没有这类程序的编程经验,需要边学习,边编程,遇到问题再网上查资料。如果没有ChatGPT,估计至少需要5天时间。
从登录开始模拟,直到完成任务,中间遇到了非常多的问题,其中耽误时间最长,与ChatGPT讨论最多的,就是下面记录的问题。
问题描述:为了规范用户输入信息,网页中有一个input框,随着用户输入字符到某个单词后,会弹出一个下拉列表框,然后要求用户单击表中列表项完成输入。遇到的问题是模拟了输入后始终不弹出下拉列表框。
下面是与ChatGPT解决问题的过程记录。

=========================================
1问:网页中,这个input是可以跟踪输入的,当输入字符后,有下拉列表框,再单击其中的列表项

ChatGPT:

如果你需要在输入后选择下拉列表框中的项,可以按照以下步骤进行操作:

  1. 输入文本: 使用 send_keys() 方法向输入框输入文本。
  2. 等待下拉列表框出现: 使用显式等待 (WebDriverWait) 等待下拉列表框的出现。
  3. 选择列表项: 单击所需的列表项。

下面是更新后的示例代码,假设下拉列表框的选择项使用 li 元素来表示:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并输入文本
    input_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.clear()  # 清空输入框内容
    input_element.send_keys('Example location')

    # 等待下拉列表框出现
    dropdown_list = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'ul.dropdown-list'))  # 替换为实际选择器
    )

    # 找到列表项并单击
    dropdown_item = WebDriverWait(dropdown_list, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'li'))  # 替换为实际选择器
    )
    dropdown_item.click()

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据实际情况替换代码中的选择器,确保能准确地定位到输入框、下拉列表框和列表项。这样就能模拟用户在输入文本后选择下拉列表框中的项,并点击提交按钮了。

===========================
2问:这个列表框有5项,需要选择的“Example location”(编者更改了真实输入字符,下面有同样的处理),排在第5项,其它项,在这个模拟程序中实际不用管

ChatGPT:

理解了,你希望在输入文本后,自动选择下拉列表中的特定项,并点击提交按钮。下面是相应的示例代码,针对你描述的情况进行了调整:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并输入文本
    input_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.clear()  # 清空输入框内容
    input_element.send_keys('Example location')

    # 等待下拉列表框出现
    dropdown_list = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'ul.dropdown-list'))  # 替换为实际选择器
    )

    # 找到并单击特定的列表项(假设 "Example location" 是第5项)
    dropdown_items = WebDriverWait(dropdown_list, 10).until(
        EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'li'))  # 替换为实际选择器
    )
    specific_item = dropdown_items[4]  # 第5项,索引从0开始
    specific_item.click()

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个示例中:

  • send_keys('Example location') 输入文本 “Example location”。
  • 等待下拉列表框出现并找到所有的列表项。
  • 选择第5项(索引为4),并点击该项。
  • 执行提交按钮的点击操作。

请根据实际情况调整代码中的选择器和逻辑,确保能正确模拟用户在网页上的输入和选择操作。

===========================
3问:这个input,手动输入“Example ”后,有正确的下拉框出现。python程序中input_element.send_keys(‘Example location’)执行后没有下拉框

ChatGPT:

如果使用 send_keys() 方法输入文本后没有正确显示下拉框,这可能是由于以下几个原因导致的:

  1. 输入速度和事件触发: 有些网页的下拉框可能需要一定的时间来响应输入内容并显示匹配的下拉选项。有时候,输入过快可能会导致下拉框没有正确显示。你可以在 send_keys() 后加入适当的等待时间。

  2. 输入内容是否匹配: 确保使用 send_keys() 输入的内容与下拉框中的选项匹配。有时输入的内容不精确可能导致下拉框没有正确显示。

  3. 页面动态加载: 如果页面使用了动态加载或者 JavaScript 来实现下拉框功能,可能需要等待页面完全加载或者使用适当的等待条件 (WebDriverWait)。

为了更精确地模拟手动输入并触发下拉框,你可以尝试以下方法:

  • send_keys() 后添加适当的等待时间,例如 time.sleep(2),确保输入后等待一段时间再进行后续操作。

  • 如果页面使用了 JavaScript 来实现下拉框的显示,可以考虑使用 execute_script() 方法来模拟触发 JavaScript 事件,例如 input_element.send_keys('Example location'); input_element.send_keys(Keys.ENTER),这会发送一个回车键,可能会触发下拉框的显示。

下面是一个调整后的示例代码,演示了添加等待时间来确保下拉框能正确显示:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并输入文本
    input_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.clear()  # 清空输入框内容
    input_element.send_keys('Example location')
    
    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 等待下拉列表框出现
    dropdown_list = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'ul.dropdown-list'))  # 替换为实际选择器
    )

    # 找到并单击特定的列表项(假设 "Example location" 是第5项)
    dropdown_items = WebDriverWait(dropdown_list, 10).until(
        EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'li'))  # 替换为实际选择器
    )
    specific_item = dropdown_items[4]  # 第5项,索引从0开始
    specific_item.click()

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据你的具体情况调整代码,并确保在输入后等待足够的时间来确保下拉框正确显示。

========================
4问:是否是需要单击一下这个input,获取焦点,再输入字符,这样才能弹出下拉框

ChatGPT:

是的,有些网页中的输入框可能需要先单击获取焦点,然后才能正确输入内容并触发下拉框的显示。这种情况下,可以通过以下方式调整代码:

  1. 先单击获取焦点: 在使用 send_keys() 方法之前,先使用 click() 方法单击输入框,确保输入框获取了焦点。

  2. 输入文本后等待: 在输入文本后,等待一段时间以确保下拉框能正确显示。

下面是一个示例代码,演示了先单击输入框获取焦点,然后输入文本并等待一段时间,最后选择特定的下拉列表项和点击提交按钮的操作:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 等待下拉列表框出现
    dropdown_list = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'ul.dropdown-list'))  # 替换为实际选择器
    )

    # 找到并单击特定的列表项(假设 "Example location" 是第5项)
    dropdown_items = WebDriverWait(dropdown_list, 10).until(
        EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'li'))  # 替换为实际选择器
    )
    specific_item = dropdown_items[4]  # 第5项,索引从0开始
    specific_item.click()

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据具体情况调整代码,确保在输入文本前单击获取焦点,并在输入后等待足够的时间来确保下拉框的正确显示和选择。

=====================
5问:修改一下代码。下拉列表框是listbox,对应

html:<div role="listbox" class="mat-autocomplete-panel ng-star-inserted mat-autocomplete-visible" id="mat-autocomplete-36">

ChatGPT:

根据你提供的 HTML 结构,下面是一个示例代码,演示如何在输入框中输入文本,并选择特定的下拉列表项:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到下拉框的父元素
    dropdown_panel = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'mat-autocomplete-panel'))  # 替换为实际选择器
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据你实际的网页结构和元素选择器进行调整。上述代码中,使用了 Selenium 的等待条件来确保元素的可见性和可点击性,以及适当的延迟来等待下拉框的显示和选项的加载。

=================
6问:Traceback (most recent call last):
File “get_appointment.py”, line 140, in
EC.visibility_of_element_located((By.CLASS_NAME, ‘mat-autocomplete-panel’)) # 替换为实际选择器
File “e:\ProgramData\Anaconda3\envs\gyroflow\lib\site-packages\selenium\webdriver\support\wait.py”, line 95, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

ChatGPT:

TimeoutException通常意味着Selenium在指定的时间内无法找到期望的元素。这可能是由于以下几个原因导致的:

  1. 元素选择器不正确: 确保使用的选择器(如CLASS_NAME、CSS_SELECTOR等)能够准确地定位到目标元素。有时候网页结构或元素的class或id可能会变化,需要根据实际情况进行调整。

  2. 页面加载时间过长: 如果页面加载速度较慢,可能会导致元素在预期时间内未能加载完成。可以增加等待的时间,或者使用隐式等待来提高稳定性。

  3. 元素确实未出现: 如果元素是动态加载的,并且在指定的等待时间内未能出现,可能需要重新审视页面加载的逻辑和元素的加载时机。

针对你的情况,建议尝试以下几点来解决问题:

  • 确认选择器是否正确: 确保By.CLASS_NAME指向的类名(如mat-autocomplete-panel)确实对应于下拉框的父元素。

  • 增加等待时间: 可以增加等待的时间,例如将WebDriverWait中的等待时间从10秒增加到20秒,看看是否能解决问题。

  • 使用隐式等待: 在创建WebDriver时,可以设置隐式等待时间,以便在查找元素时等待一定的时间,例如driver.implicitly_wait(10),这样可以在找不到元素时自动等待一段时间再重试。

下面是一个调整后的示例代码,增加了一些错误处理和等待时间的调整:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到下拉框的父元素
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'mat-autocomplete-panel'))  # 替换为实际选择器
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据具体情况调整等待时间和选择器,确保能够准确地定位到输入框、下拉框以及提交按钮。

=================
7问:Traceback (most recent call last):
File “get_appointment.py”, line 141, in
EC.visibility_of_element_located((By.ID, ‘mat-autocomplete-36’)) # 替换为实际选择器

ChatGPT:

TimeoutException通常表示Selenium在等待期间未能找到预期的元素。这可能是由于以下几个原因导致的:

  1. 选择器不正确: 确保使用的选择器能够准确地定位到目标元素。在你的情况中,使用了By.ID来定位元素,需要确保'mat-autocomplete-36'对应的ID确实存在于页面中。

  2. 页面加载时间过长: 如果页面加载速度较慢,可能会导致元素在预期时间内未能加载完成。可以尝试增加等待的时间。

  3. 元素确实未出现: 如果元素是动态加载的,并且在指定的等待时间内未能出现,可能需要重新审视页面加载的逻辑和元素的加载时机。

在你的代码中,尝试将等待时间从10秒增加到20秒可能会有所帮助。此外,也可以使用隐式等待来提高稳定性。下面是调整后的示例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到下拉框的父元素
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.ID, 'mat-autocomplete-36'))  # 替换为实际选择器
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

请根据具体情况进一步调整等待时间和选择器,确保能够准确地定位到输入框、下拉框以及提交按钮。

=====================
8问:我发现EC.visibility_of_element_located((By.ID, ‘mat-autocomplete-36’))这个ID中的数值36,每次运行时可能有变化,能否程序中查找<div role=“listbox” …>

ChatGPT:

可以通过查找具有 role="listbox" 属性的 <div> 元素来定位下拉列表。这样可以避免使用动态变化的ID。以下是修改后的代码示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到具有 role="listbox" 属性的 <div> 元素
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]'))
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个代码中,我们通过 EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]')) 定位到具有 role="listbox" 属性的 <div> 元素,然后找到其下的所有 <mat-option> 元素,遍历这些选项并找到匹配目标文本的选项,最后进行点击操作。

请根据具体情况进一步调整选择器和等待时间,确保能够准确地定位到输入框、下拉框以及提交按钮。

=======================
9问:问题还是出在input_element.send_keys(‘Example location’)这里,在input元素中输入了字符后没有激活弹出下拉框,这里应该有一个交互过程,因为输入的字符不同,下拉框中的列表项就不同,但程序中没有这个交互过程,而只是等待出现下拉框,应该有错

ChatGPT:

为了触发下拉框的出现,可以尝试在输入框输入字符后,模拟按下键盘上的箭头键或回车键。这种方式可以强制触发下拉框的显示。以下是修改后的代码示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本
    input_element.send_keys('Example location')

    # 模拟按下箭头键或回车键,以触发下拉框
    input_element.send_keys(Keys.DOWN)
    time.sleep(1)  # 等待下拉框显示
    input_element.send_keys(Keys.DOWN)
    time.sleep(1)
    input_element.send_keys(Keys.RETURN)

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到具有 role="listbox" 属性的 <div> 元素
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]'))
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个代码中,input_element.send_keys(Keys.DOWN)input_element.send_keys(Keys.RETURN) 用于模拟用户按下键盘上的箭头键和回车键,从而触发下拉框的显示。根据需要,可以增加或减少按键的次数。确保在执行这些操作之后添加适当的等待时间,以便下拉框能够正确显示。

请根据具体情况进一步调整选择器和等待时间,确保能够准确地定位到输入框、下拉框以及提交按钮。

================
10问:input_element.send_keys(‘Example’),然后模拟空格键,再input_element最后增加一个空格符,代码如何写?

ChatGPT:

好的,我们可以修改代码为 input_element.send_keys('Example'),然后模拟空格键,并在 input_element 最后增加一个空格符。以下是修改后的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本 'Port'
    input_element.send_keys('Example')

    # 模拟按下空格键,以触发下拉框
    input_element.send_keys(Keys.SPACE)

    # 在输入框最后增加一个空格符
    input_element.send_keys(' ')

    # 等待一段时间,确保下拉框能正确显示
    time.sleep(2)

    # 找到具有 role="listbox" 属性的 <div> 元素
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]'))
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个代码中,input_element.send_keys('Example') 后,通过 input_element.send_keys(Keys.SPACE) 模拟按下空格键,然后再通过 input_element.send_keys(' ') 在输入框最后增加一个空格符。这个过程模拟了用户的输入操作,并且等待下拉框出现。

=============================
11问:程序执行仍然不弹出下拉框,但这时手动输入空格,可以弹出下拉框,说明程序模拟有问题

ChatGPT:

如果手动输入空格可以弹出下拉框,而程序模拟输入空格不行,可能是因为模拟输入的速度太快,或者需要一些其他的交互来触发下拉框弹出。我们可以尝试以下步骤:

  1. 输入 ‘Port’ 后,等待一下,然后再输入空格
  2. 通过JavaScript触发输入事件,以确保下拉框出现。

请尝试以下代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 输入文本 'Port'
    input_element.send_keys('Example')

    # 等待一下,让前面的输入生效
    time.sleep(1)

    # 使用JavaScript触发input事件
    driver.execute_script("arguments[0].value = arguments[0].value + ' '", input_element)
    driver.execute_script("arguments[0].dispatchEvent(new Event('input', { bubbles: true }))", input_element)

    # 等待下拉框出现
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]'))
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个代码中,driver.execute_script("arguments[0].value = arguments[0].value + ' '", input_element) 用于在输入框中添加一个空格,并通过 driver.execute_script("arguments[0].dispatchEvent(new Event('input', { bubbles: true }))", input_element) 触发输入事件,确保下拉框出现。

====================
12问:仍然没有出现下拉框

ChatGPT:

如果模拟输入仍然无法触发下拉框出现,可以尝试添加更多的延迟或更复杂的模拟输入交互过程。以下是进一步的调整:

  1. 添加延迟:在每个键输入之间添加延迟,确保每个字符输入后有足够的时间让下拉框响应。
  2. 逐字符输入:逐字符输入 ‘Example location’,在每个字符输入后都模拟一个输入事件。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
import time

# 设置WebDriver
driver = webdriver.Chrome(ChromeDriverManager().install())

try:
    # 打开目标网站
    driver.get('https://example.com')  # 替换为实际的URL

    # 找到输入框并单击获取焦点
    input_element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.ID, 'mat-input-3'))  # 替换为实际选择器
    )
    input_element.click()

    # 逐字符输入 'Example location'
    text_to_type = 'Example location'
    for char in text_to_type:
        input_element.send_keys(char)
        time.sleep(0.2)  # 添加延迟,确保每个字符输入后有足够的时间

        # 使用JavaScript触发input事件
        driver.execute_script("arguments[0].dispatchEvent(new Event('input', { bubbles: true }))", input_element)

    # 再次等待一下,确保输入生效
    time.sleep(1)

    # 使用空格触发下拉框
    input_element.send_keys(Keys.SPACE)
    driver.execute_script("arguments[0].dispatchEvent(new Event('input', { bubbles: true }))", input_element)

    # 等待下拉框出现
    dropdown_panel = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[role="listbox"]'))
    )

    # 找到所有下拉选项
    dropdown_items = dropdown_panel.find_elements(By.CSS_SELECTOR, 'mat-option')

    # 遍历下拉选项,找到并单击包含特定文本的选项
    target_text = "Example location"
    for option in dropdown_items:
        option_text = option.find_element(By.CSS_SELECTOR, '.mat-option-text').text.strip()
        if option_text == target_text:
            option.click()
            break

    # 找到并单击提交按钮
    submit_button = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.mat-raised-button')) 
    )
    submit_button.click()

    # 等待页面加载或执行其他操作...

finally:
    # 关闭浏览器
    driver.quit()

在这个代码中,每个字符输入后都会触发 input 事件,并且添加了适当的延迟,以确保输入的每个字符都有足够的时间被处理,从而使下拉框能够正常弹出。

============================
最终解决问题。

  • 36
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值