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")
,但没有任何变化,最后使用 x 和 y 坐标单击:action.move_by_offset(10,10).click().perform()
再次,没有任何反应。
解决方案
您是否尝试使用正确的值更改 cookie?因为那似乎是存储国家和货币的地方?
推荐阅读
- internationalization - 试图从全局命名空间加载类“IntlTimeZone”
- java - 从活动外部捕获 onRequestPermissionsResult()
- php - Symfony 4 用户通过电子邮件问题进行搜索
- reactjs - glyphicon 在 JSX 中添加边距
- sql-server - 使用家庭护理机构数据进行实时项目的步骤是什么……例如实施数据库和 BI 解决方案,然后进行报告?
- python - 创建具有动态参数的类部分对象
- java - 根据类型参数自动装配 CrudRepository
- jquery - jquery this.each 在使用 .val 时不起作用
- sql - 如何使用连接更新表
- r - 在 R 中使用函数 TOLOWER