The problem: I want to search keywords on Amazon and take screenshots. I am using selenium package. However, when I search on amazon.co.uk, it shows delivery address as Unites States. How can I change the "Deliver to Country"?
Below are sample Python code and a sample screenshot.
import time as t
from datetime import datetime
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("user-agent=UA")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--headless")
chrome_options.add_argument('window-size=2160x3840')
urls = ['https://www.amazon.co.uk/s?k=advil', 'https://www.amazon.co.uk/s?k=Whitening toothpaste']
def get_secondly_screenshots(navi_dictionary):
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get('https://www.amazon.co.uk')
driver.execute_script("document.body.style.zoom='50%'")
driver.get(url)
try:
test = driver.find_element('xpath', '//*[@id="sp-cc-rejectall-link"]')
test.click()
print('gotcha!')
except:
pass
now = datetime.now()
date_time = now.strftime("%Y_%m_%d_%H_%M_%S")
sh_url = url.split('?k=')[1]
print(sh_url, date_time)
driver.save_screenshot(f'{sh_url}_{date_time}.png')
print('screenshotted ', url)
t.sleep(2)
driver.quit()
for url in urls:
get_secondly_screenshots(url)
CodePudding user response:
In order to set UK delivery address on UK Amazon when your IP address is out from the UK you can do the following steps:
- Open the "Delivery to" dialog
- Insert some valid UK postal code and click submit button
- Approve this on the appeared after that pop-up.
The following Selenium code performs that
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
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://www.amazon.co.uk/s?k=advil"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.ID, 'nav-global-location-popover-link'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[data-action='GLUXPostalInputAction']"))).send_keys("PO16 7GZ")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[aria-labelledby='GLUXZipUpdate-announce']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".a-popover-footer #GLUXConfirmClose"))).click()
In case you have any questions about my code - don't hesitate to ask.
CodePudding user response:
Isn't it enough to interact with that switch initially and set the desired country?
Let me make a few changes to your code, which I explain below, to make it perform better for this task.
- It is necessary to resolve this depreciation notice: 'DeprecationWarning: executable_path has been deprecated, please pass in a Service object'.
- You can instantiate the driver, reject cookies, and change the country of delivery only once and not at each iteration.
- You have to integrate some driver wait (and not sleeps) to prevent the page from still not loading properly and the script from failing. The "DELAY" parameter you can configure based on the speed of your connection, the heaviness of the page, and the performance of your pc.
- You asked to change the country of delivery, so just select the corresponding value in the drop-down menu with Select. You can change the country simply by changing that string.
import time as t
from datetime import datetime
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
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.common.exceptions import TimeoutException
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("user-agent=UA")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--headless")
chrome_options.add_argument('window-size=2160x3840')
DELAY = 20 # Number of seconds before timing out
def get_secondly_screenshots(driver, url):
# got to current url
driver.get(url)
now = datetime.now()
date_time = now.strftime("%Y_%m_%d_%H_%M_%S")
sh_url = url.split('?k=')[1]
print(sh_url, date_time)
driver.save_screenshot(f'{sh_url}_{date_time}.png')
print('screenshotted ', url)
if __name__ == '__main__':
urls = ['https://www.amazon.co.uk/s?k=advil', 'https://www.amazon.co.uk/s?k=Whitening toothpaste']
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# first go to amazon site
driver.get('https://www.amazon.co.uk')
# You can reject cookies once instead of each iteration
try:
cookie_btn = WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="sp-cc-rejectall-link"]')))
cookie_btn.click()
except TimeoutException:
raise TimeoutException("Page not yet loaded correctly")
# You can change delivery country once (even here instead of each iteration)
WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.ID, 'nav-global-location-popover-link'))).click()
WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.ID, 'GLUXCountryList')))
select = Select(driver.find_element(By.ID, 'GLUXCountryList'))
# select country by value (e.g. 'UK')
select.select_by_value('IT')
for url in urls:
get_secondly_screenshots(driver, url)
driver.quit()
If you also want to change the zip code, you have to do the step shown by @Prophet.