【问题标题】:Element is not located selenium python元素不位于 selenium python
【发布时间】:2018-02-12 04:29:09
【问题描述】:

我有 selenium 脚本,其基本目标是将位置放在输入标签中,然后单击搜索按钮并打开结果页面,但我在查找输入和搜索按钮时遇到问题 脚本如下

# -*- coding: utf-8 -*-

import csv
from lxml import html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def search_location():
    for typ in TYPESOFR:
        for loc in LOCATIONS:
            MAINBROWSER.get(typ)
            elm = WebDriverWait(MAINBROWSER, 10).until(EC.presence_of_element_located((By.ID, 'localisation')))         
            location = MAINBROWSER.find_element_by_id("localisation")
            location.click()
            location.send_keys(loc)
            search = MAINBROWSER.find_element_by_xpath('.//button[@class="sendsearch btn-blue"]')
            MAINBROWSER.execute_script("arguments[0].click();", search)

def main():
    search_location()


if __name__ == '__main__':
    # Links of types of real estates
    TYPESOFR = [
        'https://www.immoweb.be/nl/immo/huis/te-huur',
        'https://www.immoweb.be/nl/immo/appartement/te-huur',
        'https://www.immoweb.be/nl/immo/handelspand/te-huur',
        'https://www.immoweb.be/nl/immo/kantoor/te-huur',
        'https://www.immoweb.be/nl/immo/industrie/te-huur',
        'https://www.immoweb.be/nl/immo/garage/te-huur',
        'https://www.immoweb.be/nl/immo/ander/te-huur',]

    LOCATIONS = ['9000', '2000', '1000']


    chromeOptions = webdriver.ChromeOptions()
    # Disable image loading on page it will load page faster
    prefs = {"profile.managed_default_content_settings.images":2}
    chromeOptions.add_experimental_option("prefs",prefs)
    MAINBROWSER = webdriver.Chrome(chrome_options=chromeOptions)

    # MAINBROWSER = webdriver.Firefox()

    # BROWSER = webdriver.Chrome()
    main()

代码基本上是从TYPESOFR列表中获取一个URL并打开链接并从LOCATIONS列表中获取一个位置并将其放入输入标签中然后单击搜索按钮 并按照步骤操作,直到循环完成

我在 Chrome 和 Firefox 中都尝试过,都给出了相同的错误,即元素未定位

【问题讨论】:

  • 没有找到哪个元素——您的代码中有几个查找。另外,您可以发布具有该元素的 html 示例吗?我们没有什么要验证的。
  • https://www.immoweb.be/nl/immo/huis/te-huur 这是 url 并且有 iframe id="IWEB_IFRAME_ID_SEARCH" 它有一个页面包含输入标签和 id 是 id="localisation"

标签: python selenium selenium-chromedriver geckodriver


【解决方案1】:

html中有iframe标签,需要使用web驱动的switch_to功能

在上述情况下,它是这样使用的

# Switching to searh iFrame
MAINBROWSER.switch_to.frame(MAINBROWSER.find_element_by_xpath('.//*[@id="IWEB_IFRAME_ID_SEARCH"]'))

# no input tag can be located by existing line
location = MAINBROWSER.find_element_by_id("localisation")
# ....... other logics ........

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多