[英]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.