我有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中都尝试过同样的错误,即元素不在位
答案 0 :(得分:1)
html中有iframe标签,因此需要使用web驱动程序的switch_to功能
在上面的例子中,它就像这样使用
private void OnClicked(object sender, EventArgs e)
{
if(PBIndicator==false){
activityIndicator.IsRunning=true;
}else{
activityIndicator.IsRunning=false;
}
}