如何使用 Selenium Python 接受 cookie 同意
我正在尝试使用 Selenium Python 接受 cookies 同意。我尝试使用 CSS 选择器和 XPath 进行搜索,但没有任何效果。 这是 HTML:
<button class="sc-1epc5np-0 dnGUzk sc-f7uhhq-2 coEmEP button button--filled button__acceptAll" type="button"><span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span></button>
<span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span>
</button>
我尝试了以下代码:
WebDriverWait(driver, 40).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.sc-1vlt5h-0.sc-1epc5np-1.cMLEOX.baseText')))
我也尝试过:
driver.find_element_by_css_selector("cMLEOX").click()
driver.find_element_by_css_selector(".cMLEOX").click()
没有任何作用。解决办法是什么?
I am trying to accept cookies consent with Selenium Python. I tried to search with CSS selector and XPath but nothing works.
This is the HTML:
<button class="sc-1epc5np-0 dnGUzk sc-f7uhhq-2 coEmEP button button--filled button__acceptAll" type="button"><span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span></button>
<span theme="[object Object]" class="sc-1vlt5h-0 sc-1epc5np-1 cMLEOX baseText">Accept Cookies</span>
</button>
I tried the following code :
WebDriverWait(driver, 40).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.sc-1vlt5h-0.sc-1epc5np-1.cMLEOX.baseText')))
I also tried:
driver.find_element_by_css_selector("cMLEOX").click()
driver.find_element_by_css_selector(".cMLEOX").click()
Nothing works. What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所需的元素是动态元素,因此要在元素上 click() 而不是 presence_of_element_ located() 您需要为 引入 WebDriverWait href="https://stackoverflow.com/a/54194511/7429447">element_to_be_clickable() 并且您可以使用以下任一定位策略:
使用CSS_SELECTOR:
使用XPATH:
注意:您必须添加以下导入:
The desired element is a dynamic element, so to click() on the element instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
Using XPATH:
Note: You have to add the following imports :