简体   繁体   English

下拉列表选择使用 Selenium Python

[英]Drop-down list selection using Selenium Python

I'm trying to select "Manhattan", "WINDSOR SQUARE", and "Wall St."我正在尝试 select “曼哈顿”、“温莎广场”和“华尔街”。 from the "city", "Neighborhood", and "Collections" dropdown search boxes respectively on this website: https://upxland.me/properties/ .分别来自本网站的“城市”、“社区”和“收藏”下拉搜索框: https://upxland.me/properties/ The same code (except the text I'm selecting) works for the "City" dropdown but not "Neighborhood", and "Collections".相同的代码(我选择的文本除外)适用于“城市”下拉菜单,但不适用于“邻里”和“收藏”。 Where am I doing wrong and what is the correct way to select dropdown search boxes like this?我哪里做错了,像这样的 select 下拉搜索框的正确方法是什么?

I'm also not able to locate the "download" button by copying the XPATH of it.我也无法通过复制它的 XPATH 找到“下载”按钮。

The error messages are all ElementClickInterceptedException shown below:错误消息都是 ElementClickInterceptedException 如下所示:

"raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element... is not clickable at point (476, 234). Other element would receive the click: ... (Session info: chrome=103.0.5060.134)" “raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: 消息:元素点击被截获:元素...在点 (476, 234) 不可点击。其他元素将收到点击:... (会话信息:chrome=103.0.5060.134)”

My code is shown below.我的代码如下所示。 Could anyone help?有人能帮忙吗?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import time

PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://upxland.me/properties/")

'''City'''
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(text(),'City')]"))).click()

city = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Manhattan')]")))
city.click()

'''Neighborhood'''
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(text(),'Neighborhood')]"))).click()

neighborhood = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'WINDSOR SQUARE')]")))
neighborhood.click()

'''Collection'''
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(text(),'Collections')]"))).click()

neighborhood = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Wall St.')]")))
neighborhood.click()

'''download'''
download_button_path = '//*[@id="HackerLovesUPXLand"]/div[1]/main/div/div/div/div[5]/div/div[1]/div/div[4]/button/span/i'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, download_button_path))).click()

The options in the dropdown menus for this app are loaded dinamically.此应用程序的下拉菜单中的选项是动态加载的。
it means, they are not loaded by default in DOM.这意味着,默认情况下它们不会加载到 DOM 中。

So, in order to click the option, it must be present in DOM before.因此,为了单击该选项,它必须之前存在于 DOM 中。 To achieve that, you can type the option inside the ddm's input element.为此,您可以在 ddm 的输入元素中键入选项。

For each dropdown menu you may need 3 elements对于每个下拉菜单,您可能需要 3 个元素

  • Dropdown menu xpath --> //label[text()='Neighborhood']//parent::div下拉菜单 xpath --> //label[text()='Neighborhood']//parent::div
  • Input element xpath --> //label[text()='Neighborhood']//following-sibling::div/input输入元素 xpath --> //label[text()='Neighborhood']//following-sibling::div/input
  • Option xpath --> // [text()='%s']//ancestor:: [@role='option']选项 xpath --> // [text()='%s']//ancestor:: [@role='option']

*notice the xpath are parametrized in order to make the code flexible for any option *注意 xpath 已参数化,以使代码对任何选项都灵活

Here is a code snippet (in java tho) that is currently working for selecting 'WESTWOOD PARK' in 'Neighborhood' ddm这是一个代码片段(在 java 中),目前正在为在“Neighborhood”ddm 中选择“WESTWOOD PARK”而工作

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Launcher {

    private static final String DDM_NEIGHB_XPATH = "//label[text()='Neighborhood']//parent::div";
    private static final String INPUT_NEIGH_XPATH = "//label[text()='Neighborhood']//following-sibling::div/input";
    private static final String OPTION_GENERIC_XPATH = "//*[text()='%s']//ancestor::*[@role='option']";
    private static WebDriver driver;

    public static void main(String[] args) {

        //initialize driver
        initDriver();

        //go to url
        driver.get("https://upxland.me/properties");

        //select Neighborhood
        selectNeighborhood("WESTWOOD PARK");

    }



    //actions
    public static void selectNeighborhood(String option) {
        
        //wait until the page loads and the ddm is visible
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(DDM_NEIGHB_XPATH)));

        //click in ddm
        getNeightDDM().click();

        //input option
        getNeightInput().sendKeys(option);
        
        //click option
        getNeightOption(option).click();
        
        //clear-up input
        getNeightInput().clear();

    }


    //webelements
    public static WebElement getNeightDDM() {
        return driver.findElement(By.xpath(DDM_NEIGHB_XPATH));
    }

    public static WebElement getNeightInput() {
        return driver.findElement(By.xpath(INPUT_NEIGH_XPATH));
    }

    public static WebElement getNeightOption(String option) {
        return driver.findElement(By.xpath(String.format(OPTION_GENERIC_XPATH, option)));
    }

    //setup
    private static void initDriver() {
        System.setProperty("webdriver.chrome.driver", "c:/utils/selenium/drivers/chromedriver.exe");
        driver = new ChromeDriver();
    }
}

if you Press F12 to check, the dropdown of Neighborhood is not same as city, it can support mulitple selection, and more importantly is that, the dropdown list is dynamic loaded, you need first scroll to load all items,then select it.如果你按F12查看,Neighborhood的下拉菜单和city不一样,它可以支持多选,更重要的是,下拉列表是动态加载的,你需要先滚动加载所有项目,然后select它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM