首页 > 解决方案 > 元素仅在手动单击页面 selenium python 时可用

问题描述

这种网络抓取已经可以改变全球速卖通的国家和货币,但前提是我在弹出窗口出现之前手动点击页面,我不明白为什么会这样。

这是我的代码:

from selenium import webdriver
from os import getcwd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


option = webdriver.ChromeOptions()
prefs = {
    "translate_whitelists": {"es": "en"},
    "translate": {"enabled": "true"},
    "profile.default_content_setting_values.notifications": 2
}
option.add_experimental_option("prefs", prefs)
option.add_argument("start-maximized")
option.add_argument("--lang=en")


driver = webdriver.Chrome(executable_path=f"{getcwd()}\chromedriver.exe", options=option)
driver.switch_to.window(driver.current_window_handle)
url = input("Type a aliexpress url: ")
driver.get(url)


# close aliexpress coupon pop-up
WebDriverWait(driver, 20).until(
    EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src, '__poplayer')]")))
time.sleep(5)
for i in driver.find_elements_by_xpath("//img[contains(@src, 'http')]"):
    i.click()
driver.switch_to_default_content()


# change country to india and currençy to usd
driver.find_element_by_xpath("//a[contains(@class, 'switcher-info')]/span[@class='ship-to']/i").click()
time.sleep(5)
driver.find_element_by_class_name("switcher-common").find_elements_by_tag_name("a")[0].click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
    (By.XPATH, "//li[@class='address-select-item']//span[@class='shipping-text' and text()='India']"))).click()
driver.find_element_by_class_name("switcher-common").find_elements_by_class_name("switcher-currency-c")[1].click()
driver.find_elements_by_xpath("//div[@class='search-container']//input[@class='search-currency']")[1].send_keys("usd")
driver.find_element_by_xpath("//a[@data-currency='USD']").click()
driver.find_element_by_class_name("switcher-common").find_elements_by_tag_name("button")[0].click()

当我不点击时,它会返回此错误:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

我已经尝试单击顶部的导航栏等随机位置,但它返回一个错误,指出弹出窗口已将其遮挡。因此,当弹出窗口出现在正文中时,我尝试使用:单击它 driver.find_element_by_css_selector("body"),但没有任何变化。

标签: pythonselenium

解决方案


推荐阅读