英文:
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.
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
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论