[英]How can I click on multiple drop down list on a page with Python and Selenium?
[英]Drop down list and Selenium with Python
我正在嘗試開發代碼以在能源比較網站上進行數據刮取。 問題在於該網站中包含一些下拉列表(也許是AJAX,但我不知道),因此即使我在提交信息時鍵入正確的名稱,該頁面也會給我兩個錯誤。
-插入您的房屋表面(它不應該告訴我,因為當我按“我知道我的消費量”時,它已經消失了) [未完成]
-您必須插入您的城市(即使我輸入了全名,也無法從硒中單擊下拉列表中的某個項目)[解決]
這是代碼:
from bs4 import BeautifulSoup as soup
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Font
from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime
import os
#saving url
browser = webdriver.Chrome(executable_path=r"C:\Program Files(x86)\Google\Chrome\Application\chromedriver.exe")
my_url = 'https://comparateur.selectra.info'
#opening my connection and downloading the page
browser.get(my_url)
button = browser.find_elements_by_class_name('custom-control-indicator')[-1]
browser.execute_script("arguments[0].click();", button)
start = browser.find_element(By.XPATH, '//*[@id=\"no-cookie\"]/form/button')
start.click()
comp = browser.find_element(By.XPATH, '//*[@id=\"form-comparaison\"]/fieldset[1]/div[1]/div/label[1]')
comp.click()
cons = browser.find_element_by_class_name('know_consumption_yes')
browser.execute_script("arguments[0].click();", cons)
browser.execute_script("arguments[0].click();", cons)
kw = browser.find_element_by_id('annual-elec')
kw.send_keys("3200")
ville = browser.find_element_by_id('city')
ville.send_keys("Paris (75001)")
submit = browser.find_element_by_id('compare-offer')
submit.click()
提前致謝
我不確定您的選擇器是否正確,因為沒有html,
嘗試這些方法,它們應該主要解決問題,
driver.get('url')
select = Select(driver.find_element_by_id('your_select_element_id here'))
# select by visible text
select.select_by_visible_text('Your option text here')
# select by value
select.select_by_value('option_value here')
另外,請參考此問題的答案
列表在這里:
只是看看你需要什么。
首先,您必須在輸入字段中輸入名稱(更好的全名),然后稍等一會(2-3秒),列表就會出現,然后您可以從丟失的下拉列表中找到您的元素,然后單擊它。
例如:
ville = browser.find_element_by_id('city')
ville.send_keys("Paris (75001)")
driver.implicitly_wait(2) # waits 2 sec
element = driver.find_element_by_xpath("//*[contains(text(), 'Paris (75001)')]") # locate the dropdown elem
element.click() # click on it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.