亚马逊网站显示“送货到国家”,我如何在Python Selenium中通过编程更改它来截图

kokeuurv  于 2022-11-24  发布在  Python
关注(0)|答案(2)|浏览(137)

问题在于:我想在亚马逊上搜索关键词并截图,我使用的是 selenium 包,但是在www.example.com上搜索时amazon.co.uk,送货地址显示为Unites States,如何更改“Deliver to Country”?
下面是Python代码示例和屏幕截图示例。

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)

qoefvg9y

qoefvg9y1#

当您的IP地址不在英国时,为了在英国亚马逊上设置英国送货地址,您可以执行以下步骤:
1.打开“交货地点”对话框
1.插入一些有效的英国邮政编码,然后单击“提交”按钮
1.在弹出窗口后出现的上批准此。
1.正如您所要求的,我还添加了关闭cookie横幅的代码,然后单击“继续”。
下面的Selenium代码执行该操作

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()
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".glow-toaster-footer input[data-action-type='DISMISS']"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "sp-cc-accept"))).click()

此代码的结果为

如果您对我的代码有任何疑问-请不要犹豫。

mctunoxg

mctunoxg2#

最初与该开关交互并设置所需的国家/地区还不够吗?
让我对您的代码做一些修改,我将在下面解释,以使它更好地执行此任务。
1.有必要解决此折旧通知:* '过时警告:executable_path已过时,请传入服务对象'*。
1.您可以示例化驱动程序、拒绝Cookie以及仅更改一次交付国家/地区,而不是在每次迭代时更改。
1.你必须集成一些驱动程序wait(和不休眠),以防止页面仍然不能正常加载和脚本失败。“延迟”参数,你可以配置的基础上,你的连接速度,网页的沉重性,和你的pc的性能。
1.您要求更改交货国家/地区,因此只需在下拉菜单中选择相应的值Select。您可以通过更改该字符串来更改国家/地区。但是,如果您想从www.example.com输入UKamazon.co.uk(尚未选择),则必须输入邮政编码。

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()

要继续使用UK,您必须注解掉Select部分,并在主体中实现以下代码:

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'))

    # set an UK zipcode to foce UK delivery
    WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.ID, "GLUXZipUpdateInput"))).send_keys("E1W 2RG")
    WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.ID, "GLUXZipUpdate"))).click()
    WebDriverWait(driver, DELAY).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[aria-labelledby='GLUXZipUpdate-announce']"))).click()

    for url in urls:
        get_secondly_screenshots(driver, url)

    driver.quit()

相关问题