How to select a country from https://www.aliexpress.com/ ship to drowdown menu using Selenium and Python

蹲街弑〆低调 提交于 2020-12-21 03:55:51

问题


On the website https://www.aliexpress.com, I need to change the country from the dropdown menu using selenium

<span class="ship-to">

I can't find how I click on the country value using selenium


回答1:


From the Ship to drop-down-menu to select the country as Afghanistan you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:

  • Code Block:

    driver.get("https://www.aliexpress.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'switcher-info')]/span[@class='ship-to']/i"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='address-select-trigger']//span[@class='css_flag css_in']//span[@class='shipping-text']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='address-select-item ']//span[@class='shipping-text' and text()='Afghanistan']"))).click()
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:




回答2:


you can use ActionChains. Element is your xpath

from selenium.webdriver import ActionChains
actions = ActionChains(browser)
actions.move_to_element(element).perform()
actions.click().perform()

You can read more about ActionChains here https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

or you can use click() function example:

country_button = browser.find_element_by_class_name('ship-to')
country_button.click()



回答3:


    import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class aliexpress {

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver83\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();


        driver.manage().window().maximize();
        driver.get("https://www.aliexpress.com/");
        driver.findElement(By.xpath("//span[@class='ship-to']")).click();
        driver.findElement(By.xpath("//div[@id='nav-global']/div[4]/div/div/div/div/div/a")).click();

        List <WebElement> lists=driver.findElements(By.xpath("//ul[@data-role='content']//li"));
        System.out.println(lists.size());

        for (int i = 0; i < lists.size(); i++) {
            //System.out.println(LIST.get(i).getText());
            if (lists.get(i).getText().contains("Barbados")) {
                lists.get(i).click();
                break;
            }


        }
    }
}


来源:https://stackoverflow.com/questions/62993911/how-to-select-a-country-from-https-www-aliexpress-com-ship-to-drowdown-menu-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!