繁体   English   中英

Python Selenium单击可见的元素

[英]Python Selenium click an element if visible

我正在使用Python Selenium尝试检查某个元素是否可见,然后单击(如果存在)...

# Check to see if element is visible
    myelement = driver.find_element_by_xpath("//a[@id='form1']")

    if myelement.is_displayed():
        print (" ")
    else:
        driver.find_element_by_xpath("//a[@id='form1']").click

这不起作用,我要去哪里错了?

假设您的xpath正确,则应使用click()而不是click 这是一种方法,而不是属性。

你有两个问题

  • click是一种方法,应该是click()
  • 当前,您正在尝试单击是否显示按钮。 它应该是

     if myelement.is_displayed(): driver.find_element_by_xpath("//a[@id='form1']").click() else: print (" ") 

您也不必重新定位元素即可单击它

myelement = driver.find_element_by_xpath("//a[@id='form1']")

if myelement.is_displayed():
    myelement.click()
else:
    print (" ")

最好的方法是创建一个基类,然后重新定义click and find方法,并改用以下方法:

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




class LocatorMode:

 XPATH = "xpath"
 CSS_SELECTOR = "cssSelector"
 NAME = "name"
 ID = "id"
 TAG_NAME = "tagName"


class BasePage(object):

 def __init__(self, driver):
     self.driver = driver


 def wait_for_element_visibility(self, waitTime, locatorMode, Locator):
     element = None
     if   locatorMode == LocatorMode.ID:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.ID, Locator)))
     elif locatorMode == LocatorMode.NAME:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.NAME, Locator)))
     elif locatorMode == LocatorMode.XPATH:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.XPATH, Locator)))
     elif locatorMode == LocatorMode.CSS_SELECTOR:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.visibility_of_element_located((By.CSS_SELECTOR, Locator)))
     else:
         raise Exception("Unsupported locator strategy.")
     return element


 def wait_until_element_clickable(self, waitTime, locatorMode, Locator):
     element = None
     if   locatorMode == LocatorMode.ID:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.element_to_be_clickable((By.ID, Locator)))
     elif locatorMode == LocatorMode.NAME:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.element_to_be_clickable((By.NAME, Locator)))
     elif locatorMode == LocatorMode.XPATH:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.element_to_be_clickable((By.XPATH, Locator)))
     elif locatorMode == LocatorMode.CSS_SELECTOR:
          element = WebDriverWait(self.driver, waitTime).\
                  until(EC.element_to_be_clickable((By.CSS_SELECTOR, Locator)))
     else:
         raise Exception("Unsupported locator strategy.")
     return element



 def find_element(self, locatorMode, Locator):
     element = None
     if locatorMode == LocatorMode.ID:
        element = self.driver.find_element_by_id(Locator)
     elif locatorMode == LocatorMode.NAME:
        element = self.driver.find_element_by_name(Locator)
     elif locatorMode == LocatorMode.XPATH:
        element = self.driver.find_element_by_xpath(Locator)
     elif locatorMode == LocatorMode.CSS_SELECTOR:
        element = self.driver.find_element_by_css_selector(Locator)
     else:
        raise Exception("Unsupported locator strategy.")
     return element


 def fill_out_field(self, locatorMode, Locator, text):
     self.find_element(locatorMode, Locator).clear()
     self.find_element(locatorMode, Locator).send_keys(text)

 def click(self, waitTime, locatorMode, Locator):
     self.wait_until_element_clickable(waitTime, locatorMode, Locator).click()

您也可以尝试/除外:

try:
    driver.find_element_by_xpath("//a[@id='form1']").click()  # will click element if visible

except:
    print "Element not visible."

is_displayed()!=可见

因此,如果元素不在屏幕上,则.click()无效,但仍“显示”

正确的步骤是您必须将元素滚动到屏幕中并且可见,然后单击()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM