簡體   English   中英

元素未找到硒python

[英]Element is not located selenium python

我有一個硒腳本,該基本目標是將位置放置在輸入標簽中,然后單擊搜索按鈕並打開結果頁,但是我在查找輸入和搜索按鈕時遇到問題,腳本如下

# -*- 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中有iframe標簽,因此需要使用網絡驅動程序的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 ........

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM