Home > Back-end >  Changing country while scraping Aliexpress using python selenium
Changing country while scraping Aliexpress using python selenium

Time:09-02

I'm working on a scraping project of Aliexpress, and I want to change the ship to country using selenium,for example change spain to Australia and click Save button and then scrap the page, I already found an answer it worked just I don't know how can I save it by clicking the button save using selenium, any help is highly appreciated. This is my code using for this task :

    country_button = driver.find_element_by_class_name('ship-to')
    country_button.click()
    country_buttonn = driver.find_element_by_class_name('shipping-text')
    country_buttonn.click()     
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='address-select-item ']//span[@class='shipping-text' and text()='Australia']"))).click()  

CodePudding user response:

Well, there are 2 pop-ups there you need to close first in order to access any other elements. Then You can select the desired shipment destination. I used WebDriverWait for all those commands to make the code stable. Also, I used scrolling to scroll the desired destination button before clicking on it and finally clicked the save button.
The code below works.
Just pay attention that after selecting a new destination pop-ups can appear again.

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


options = Options()
options.add_argument("--start-maximized")

s = Service('C:\webdrivers\chromedriver.exe')

driver = webdriver.Chrome(options=options, service=s)

url = 'https://www.aliexpress.com/'

wait = WebDriverWait(driver, 10)
actions = ActionChains(driver)
driver.get(url)

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@style,'display: block')]//img[contains(@src,'TB1')]"))).click()
except:
    pass

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//img[@class='_24EHh']"))).click()
except:
    pass
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ship-to"))).click()

wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "shipping-text"))).click()

ship_to_australia_element = driver.find_element(By.XPATH, "//li[@class='address-select-item ']//span[@class='shipping-text' and text()='Australia']")
actions.move_to_element(ship_to_australia_element).perform()
time.sleep(0.5)
ship_to_australia_element.click()

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@data-role='save']"))).click()

I mostly used XPath locators here. CSS Selectors could be used as well

  • Related