Selenium Python: 如何通过点击SVG元素关闭覆盖层

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

Selenium Python: How to close the overlay by clicking on the svg element

问题

我正在寻找一种点击SVG叉叉以关闭叠加的欢迎窗口的方法。我已成功完成登录和授权,但这个叉叉让我发疯。

代码尝试:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "jss109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//svg[@class='jss109']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'svg[aria-hidden="True"]')).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "JSS109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='jss109']/*[name()='svg']")))

错误信息: TimeoutException: Message:

HTML快照:

Selenium Python: 如何通过点击SVG元素关闭覆盖层

另一个HTML快照:

Selenium Python: 如何通过点击SVG元素关闭覆盖层

英文:

I am looking for a way to click on the svg cross to close overlaying welcome window. I managed to go through login and authorization but this cross is getting me crazy.

Code trials:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "jss109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//svg[@class='jss109']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'svg[aria-hidden="True"]')).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "JSS109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@class='jss109']/*[name()='svg']")))

Error massage: TimeoutException: Message:

Snapshot of the HTML:

Selenium Python: 如何通过点击SVG元素关闭覆盖层

Another snapshot of the HTML:

Selenium Python: 如何通过点击SVG元素关闭覆盖层

答案1

得分: 1

X`_ 图标,您需要使用 WebDriverWait 来等待 element_to_be_clickable(),并且您可以使用以下任一 定位策略

  • 使用 CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h2 svg.jss109[viewBox]"))).click()
    
  • 使用 XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[.//span[starts-with(., 'Welcome to new')]]//*[name()='svg' and @class][@viewBox]"))).click()
    
  • 注意:您需要添加以下导入项:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
英文:

To click on the X icon you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h2 svg.jss109[viewBox]"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[.//span[starts-with(., 'Welcome to new')]]//*[name()='svg' and @class][@viewBox]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

huangapple
  • 本文由 发表于 2023年2月6日 05:26:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355631.html
匿名

发表评论

匿名网友

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

确定