无法定位弹出窗口按钮使用Selenium。

huangapple go评论73阅读模式
英文:

can't locate popup button with selenium

问题

这是您要翻译的内容:

"I have been trying to use selenium on a webpage but this popup is refraining me to do so.
note that the popup is only shown when you are not signed in (means you have to run my code so that selenium opens up a new browser window for you which does not have any accounts)
I want to click on the "Not Interested" button through selenium.
I don't want to close the popup every time manually, is there a way to automate this?

here is my code:

# relevant packages & modules
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

# relevant website
website = 'https://www.daraz.pk/';

# initialize Chrome
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
# open website
driver.get(website)

# maximize window
driver.maximize_window()

# waiting for popup
time.sleep(5)

# dealing with pop up

# with xpath
pop_up_deny = driver.find_element(By.XPATH, '/html/body/div[9]//div/div/div[3]/button[1]')
pop_up_deny.click()

It raised this error:

My chrome version : 110.0.5481.178 (Official Build) (64-bit)
My selenium version : ChromeDriver 110.0.5481.77"

英文:

I have been trying to use selenium on a webpage but this popup is refraining me to do so.

无法定位弹出窗口按钮使用Selenium。

note that the popup is only shown when you are not signed in (means you have to run my code so that selenium opens up a new browser window for you which does not have any accounts)

I want to click on the "Not Interested" button through selenium.

I don't want to close the popup every time manually,
is there a way to automate this?

here is my code:

# relevant packages & modules

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

import time

# relevant website
website = 'https://www.daraz.pk/'


# initialize Chrome
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')
# open website
driver.get(website)

#maximize window
driver.maximize_window()

# waiting for popup
time.sleep(5)

# dealing with pop up

# with xpath

pop_up_deny = driver.find_element(By.XPATH , '/html/body/div[9]//div/div/div[3]/button[1]')

pop_up_deny.click()

It raised this error:

无法定位弹出窗口按钮使用Selenium。

My chrome version : 110.0.5481.178 (Official Build) (64-bit)
My selenium version : ChromeDriver 110.0.5481.77

答案1

得分: 2

弹出元素位于“Shadow-root”元素内,您需要首先访问Shadow-root,然后识别“不感兴趣”按钮。

shadowRoot = driver.execute_script('return document.querySelector("div.airship-html-prompt-shadow").shadowRoot')
shadowRoot.find_element(By.CSS_SELECTOR, 'button.airship-btn.airship-btn-deny').click()
英文:

The popup element is inside Shadow-root element you need to reach to shadow-root first then identify the button not interested

shadowRoot= driver.execute_script('''return document.querySelector("div.airship-html-prompt-shadow").shadowRoot''')
shadowRoot.find_element(By.CSS_SELECTOR,"button.airship-btn.airship-btn-deny").click()

答案2

得分: 1

也许你可以给予时间来找到按钮。try-except 块提供了显示错误并使代码继续运行的功能。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    pop_up_deny_xpath = "/html/body/div[9]//div/div/div[3]/button[1]"
    pop_up_deny = WebDriverWait(driver, 20).until(
        EC.visibility_of_element_located((By.XPATH, pop_up_deny_xpath)))
    pop_up_deny.click()
except Exception as e:
    print('按钮不存在', e)
英文:

Maybe you can give time to find button. Try-except block provides to show the error and the code still continue to work.

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    pop_up_deny_xpath = "/html/body/div[9]//div/div/div[3]/button[1]"
	pop_up_deny = WebDriverWait(driver, 20).until(
					EC.visibility_of_element_located((By.XPATH, pop_up_deny_xpath)))
	pop_up_deny.click()
except Exception as e:
	print('Button does not exist', e)

huangapple
  • 本文由 发表于 2023年2月24日 14:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定