python - Selenium XPath 找到 gif 点击

标签 python selenium xpath gif

我在单击网站上定义为动画格式 (.gif) 的所有按钮时遇到问题。我在 Selenium 中使用 XPath 来查找这些按钮及其 id,但脚本不会在这一行继续。如何通过查找所有 gif 来单击所有这些按钮?

我的脚本:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait


driver = webdriver.Firefox(executable_path=r'D:\geckodriver.exe')
driver.get("http://svtbilgi.dsi.gov.tr/Sorgu.aspx")
driver.find_element_by_id("ctl00_hld1_cbHavza").click()
Select(driver.find_element_by_id("ctl00_hld1_cbHavza")).select_by_visible_text("15. Kizilirmak Havzasi")
driver.find_element_by_id("ctl00_hld1_btnListele").click()
parent_handle = driver.current_window_handle
all_urls = []
all_images = driver.find_elements_by_xpath("//div[contains(@id,'OL_Icon')]/img")
for image in all_images :
     image.click()
     for handle in driver.window_handles :
          if handle != parent_handle:
              driver.switch_to_window(handle)
              WebDriverWait(driver, 15).until(lambda d: d.execute_script('return document.readyState') == 'complete')
              all_urls.append(driver.current_url)
              driver.close()
              driver.switchTo.window(parent_handle)

最佳答案

好的,我已经完成了 ActionChains类-

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

driver = webdriver.Chrome(executable_path=r'D:\Test automation\chromedriver.exe')
driver.get("http://svtbilgi.dsi.gov.tr/Sorgu.aspx")
driver.find_element_by_id("ctl00_hld1_cbHavza").click()
Select(driver.find_element_by_id("ctl00_hld1_cbHavza")).select_by_visible_text("15. Kizilirmak Havzasi")
driver.find_element_by_id("ctl00_hld1_btnListele").click()
parent_handle = driver.current_window_handle
driver.maximize_window()
all_urls = []
all_images = WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH,"//div[contains(@id,'OL_Icon')]/img")))
print len(all_images)
for image in all_images :
     webdriver.ActionChains(driver).move_to_element(image).click(image).perform()
     for handle in driver.window_handles :
          if handle != parent_handle:
              driver.switch_to_window(handle)
              WebDriverWait(driver, 15).until(lambda d: d.execute_script('return document.readyState') == 'complete')
              all_urls.append(driver.current_url)
              driver.close()
              driver.switch_to.window(parent_handle)

print all_urls

关于python - Selenium XPath 找到 gif 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51561450/

相关文章:

python - 使用 Selenium (Python) 在 svg 中获取路径

xml - 从 str :tokenize() 循环遍历多个序列

xpath - 如何在 XPath 中连接节点的所有子节点的内容?

xpath - XPath根据子节点选择父节点

javascript - Selenium IDE 1.8.1 警告框 : cannot see the alert box while running the tests

ruby - 在 Ruby 测试单元中导入/读取参数

python - TensorFlow - Tflearning 错误 feed_dict

python - python 中的 Mysql 命令产生 : SyntaxError: EOL while scanning string literal

java - 在本地同时运行 Java 和 Python 标准

python - 在python中执行两行以上的bash代码