python - 元素仅在手动单击页面 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")
,但没有任何变化。
解决方案
推荐阅读
- python-3.x - K-Means 聚类 超参数调优
- php - 单击提交按钮后如何将表格行值传递到输入文本字段中的下一页
- ionic-framework - 如何在测试中模拟 ionBlur
- mysql - 是否可以将一些文件加载到 ecs fargate 临时存储
- firebase - Firestore 文档错误 - 在初始化程序中只能访问静态成员
- spring - 多线程中的Spring事务管理?
- android - 获取用户当前区域android
- geolocation - 我想计算离子 4 中两个位置之间的距离(以 km 为单位)。我使用了一个给我错误的函数
- python - 向模型添加 KL 散度的 Keras 问题
- kotlin - 在 Kotlin 中将“If”替换为“When”