OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I click change the country to a new one from the COUNTRY/REGION section using Selenium Webdriver

  • Thread starter Thread starter vu223
  • Start date Start date
V

vu223

Guest
I am building a web scrapper but I want to be able to switch the country to another using Selenium. For instance I want to be able to click on the country button and select a new country or region?

The code of what I have done so far The code above just scrolls to the button but is not clicking it so that i can have the option to select a different country or region. Please help.

Here is the code:

Code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import logging
import time

# Adjusting logging levels
logging.getLogger('urllib3').setLevel(logging.WARNING)
logging.getLogger('selenium').setLevel(logging.WARNING)


# Set up Selenium with Chrome in headless mode (if needed)
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')  # Uncomment if you want to run headless
#chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--disable-dev-shm-usage')

# Navigating the Gucci France Store
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options= chrome_options)
driver.get("https://www.gucci.com/za/en_gb/")

try:
    # Wait for the consent button to be present
    consent_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'onetrust-accept-btn-handler'))  
)
    consent_button.click()

    # Wait until the button containing "South Africa" is present
    south_africa_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//button[.//span[text()='South Africa']]"))
    )
    south_africa_button.click()

    #Select the United State Store
    un_store = WebDriverWait(driver, 10)(
        EC.presence_of_element_located((By.XPATH, '//a[.//span[text() = "United States"]]'))
    )
    un_store.click()

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    driver.quit()
<p>I am building a web scrapper but I want to be able to switch the country to another using Selenium.
For instance I want to be able to click on the country button and select a new country or region?</p>
<p><a href="https://i.sstatic.net/nFmIeJPN.png" rel="nofollow noreferrer">The code of what I have done so far</a>
The code above just scrolls to the button but is not clicking it so that i can have the option to select a different country or region. Please help.</p>
<p>Here is the code:</p>
<pre><code>from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import logging
import time

# Adjusting logging levels
logging.getLogger('urllib3').setLevel(logging.WARNING)
logging.getLogger('selenium').setLevel(logging.WARNING)


# Set up Selenium with Chrome in headless mode (if needed)
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless') # Uncomment if you want to run headless
#chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument('--disable-dev-shm-usage')

# Navigating the Gucci France Store
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options= chrome_options)
driver.get("https://www.gucci.com/za/en_gb/")

try:
# Wait for the consent button to be present
consent_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'onetrust-accept-btn-handler'))
)
consent_button.click()

# Wait until the button containing "South Africa" is present
south_africa_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[.//span[text()='South Africa']]"))
)
south_africa_button.click()

#Select the United State Store
un_store = WebDriverWait(driver, 10)(
EC.presence_of_element_located((By.XPATH, '//a[.//span[text() = "United States"]]'))
)
un_store.click()

except Exception as e:
print(f"An error occurred: {e}")
finally:
driver.quit()
</code></pre>
 
Top