使用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
这不起作用,我哪里错了?
答案 0 :(得分:3)
假设您的xpath是正确的,您应该使用click()
,而不是click
。这是一种方法,而不是属性。
答案 1 :(得分:3)
你有两个问题
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 (" ")
答案 2 :(得分:2)
最好的方法是创建一个基类并重新定义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()
答案 3 :(得分:0)
您也可以尝试/除外:
try:
driver.find_element_by_xpath("//a[@id='form1']").click() # will click element if visible
except:
print "Element not visible."
答案 4 :(得分:0)
is_displayed()!=可见
因此,如果元素不在屏幕上,但仍然显示"
,则.click()不起作用正确的步骤是您必须将元素滚动到屏幕并可见,然后单击()