Python Selenium 无法使用 xpath 收集元素

Python Selenium not able to collect element using xpath

这是我的 Python 代码

from selenium import webdriver
import time

driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("https://www.immoweb.be")
elem1 = driver.find_element_by_link_text('FRANCAIS')
elem1.click()
elem2 = driver.find_element_by_link_text('Maisons')
elem2.click()
time.sleep(2)
#searchBar = driver.find_element_by_xpath('//*[@id="localisation"]')
searchBar = driver.find_element_by_name('localisation')
searchbar.send_keys('hello')

我想在 "localisation" 字段中添加文本,但收到以下错误消息。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="localisation"]"}

我在其他网站上尝试了完全相同的代码,它工作正常。

您的 "localisation" 输入位于 iframe 下,这就是您无法找到它的原因

您需要调用 WebDriver.switch_to() 函数,以便在尝试与其中的元素交互之前将上下文更改为上述 iframe。

driver.switch_to.frame("IWEB_IFRAME_ID_SEARCH")

同时考虑重构您的代码以删除 sleep 并使用 Waits instead, it will make your test much faster, robust and reliable. Moreover, some elements are being loaded using AJAX technology 因此即使 Selenium "thinks" 页面加载完成,它们也可能不会立即可用。

建议的代码修改:

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

driver = webdriver.Chrome("/usr/local/bin/chromedriver")
driver.maximize_window()
driver.get("https://www.immoweb.be")
elem1 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "FRANCAIS")))
elem1.click()
elem2 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "Maisons")))
elem2.click()


driver.switch_to.frame("IWEB_IFRAME_ID_SEARCH")
searchBar = elem2 = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.NAME, "localisation")))
searchBar.send_keys('hello')
driver.quit()

要将 字符序列 发送到 localisation 字段,因为所需的元素在 <iframe> 中,因此您必须:

  • 诱导 WebDriverWait 以获得所需的 框架并切换到它.
  • 诱导 WebDriverWait 使所需的 元素可点击
  • 您可以使用以下解决方案:

    • 代码块:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
      chrome_options = webdriver.ChromeOptions() 
      chrome_options.add_argument("start-maximized")
      # chrome_options.add_argument('disable-infobars')
      driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://www.immoweb.be")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "FRANCAIS"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Maisons"))).click()
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#IWEB_IFRAME_ID_SEARCH")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#localisation"))).send_keys('Henri')
      
  • 浏览器快照:

Here you can find a relevant discussion on