如何使用Selenium单击弹出的cookie上的按钮?
嗨,我想使用此网站上的硒单击“保存服务”,以使弹出弹出消失: https:/https:/ /www.hugoboss.com/uk/home 。但是我收到一个超时例外。
import numpy as np
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
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 import webdriver
import time
driverfile = r'C:\Users\Main\Documents\Work\Projects\extra\chromedriver'
driver = webdriver.Chrome(executable_path=driverfile)
driver.get("https://www.hugoboss.com/uk/men/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(),'SAVE SERVICES')]"))).click()
更多信息:当我尝试使用类找到按钮的X_PATH时:// button [@data-testid ='uc-save button']
在“检查元素查找器”上,它返回0结果好像不存在吗?
我运行len(driver.window_handles)
加载网页并返回1后10秒,这意味着Selenium只能看到一个窗口打开。
Hi I want to click 'Save Services' using Selenium on this website to make the pop up disappear: https://www.hugoboss.com/uk/home. However I receive a timeout exception.
import numpy as np
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
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 import webdriver
import time
driverfile = r'C:\Users\Main\Documents\Work\Projects\extra\chromedriver'
driver = webdriver.Chrome(executable_path=driverfile)
driver.get("https://www.hugoboss.com/uk/men/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(),'SAVE SERVICES')]"))).click()
Further information: When I try to find the x_path of the button using class: //button[@data-testid='uc-save-button']
on the inspect element finder, It returns 0 results as if it does not exist?
I ran len(driver.window_handles)
10 seconds after the webpage was loaded and which returned 1, meaning selenium could see one window open only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的元素处于阴影根。在DevTools中查找您的元素,向上滚动,您将在DOM中看到:
进入
shadow> shadow> shadow> shadowroot
,一种简单的方法是get it get it int the parent the parent the parent the js获取对象。在该返回的对象中,您可以找到您的按钮:
edit ::从原始答案中更新了代码。这为我运行:
########
update-代码的演示:

pip List
告诉我我正在使用:Your element is in a shadow root. Find your element in devtools, scroll up and you'll see this in the DOM:
To get into the
shadowroot
, an easy way is the get it's parent item then use JS to get the object.Within that returned object you can find your button:
edit:: Updated the code from the original answer. This runs for me:
#########
Update - a demo of the code working:

pip list
tells me I'm using: