我如何解决这个问题,当我尝试点击一个在点击时展开的按钮时出现 'unable to locate the element' 错误
How do I solve this problem where I get 'unable to locate the element' error when trying to click on a button that expands on click
我正在尝试单击一个在单击时展开的按钮,但 selenium 无法找到该元素。确定的元素似乎是正确的。我在这里使用页面对象模型。
我尝试首先简单地找到元素并单击它,然后还尝试使用 ActionChains。尝试更改元素值和标识方法,例如 ID、XPath、CSS 选择器,但似乎没有任何效果。
HTML:
<tr style="border-top:1px solid #e6e6e6;"><td style="display:inline-block;"><div class="expand"><i id="expand_2971740_2086074" class="fa fa-plus-circle" onclick="" style="display: block;"></i><i style="display: none;" id="collapse_2971740_2086074" class="fa fa-minus-circle" onclick="closeBundleCourses(2971740,2086074)"></i></div></td><td class="text-center">Course1Jan</td><td class="text-center">19-06-2019</td><td class="text-center">0</td><td class="text-center">1</td></tr>
代码试验:
click_plus_button = 'expand_2971740_2086074' #id
def __init__(self,driver):
self.driver = driver
def enroll_user(self,firstname,lastname,email):
self.driver.find_element_by_link_text(self.go_to_manage_users).click()
self.driver.find_element_by_class_name(self.expand_manage_users).click()
time.sleep(2)
actions = ActionChains(self.driver)
actions.move_to_element(self.driver.find_element_by_id(self.click_plus_button)).click().perform()
self.driver.find_element_by_id(self.manage_users_firstname).send_keys(manage_users_firstname)
self.driver.find_element_by_id(self.manage_users_lastname).send_keys(manage_users_lastname)
self.driver.find_element_by_id(self.manage_users_email).send_keys(manage_users_email)
预期结果:
实际结果:
所需的元素是 动态元素 所以要 click()
在元素上你必须为 WebDriverWait =14=] 并且您可以使用以下任一项 :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.expand i.fa.fa-plus-circle[id^='expand_']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='expand']//i[@class='fa fa-plus-circle' and starts-with(@id, 'expand_')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在尝试单击一个在单击时展开的按钮,但 selenium 无法找到该元素。确定的元素似乎是正确的。我在这里使用页面对象模型。
我尝试首先简单地找到元素并单击它,然后还尝试使用 ActionChains。尝试更改元素值和标识方法,例如 ID、XPath、CSS 选择器,但似乎没有任何效果。
HTML:
<tr style="border-top:1px solid #e6e6e6;"><td style="display:inline-block;"><div class="expand"><i id="expand_2971740_2086074" class="fa fa-plus-circle" onclick="" style="display: block;"></i><i style="display: none;" id="collapse_2971740_2086074" class="fa fa-minus-circle" onclick="closeBundleCourses(2971740,2086074)"></i></div></td><td class="text-center">Course1Jan</td><td class="text-center">19-06-2019</td><td class="text-center">0</td><td class="text-center">1</td></tr>
代码试验:
click_plus_button = 'expand_2971740_2086074' #id
def __init__(self,driver):
self.driver = driver
def enroll_user(self,firstname,lastname,email):
self.driver.find_element_by_link_text(self.go_to_manage_users).click()
self.driver.find_element_by_class_name(self.expand_manage_users).click()
time.sleep(2)
actions = ActionChains(self.driver)
actions.move_to_element(self.driver.find_element_by_id(self.click_plus_button)).click().perform()
self.driver.find_element_by_id(self.manage_users_firstname).send_keys(manage_users_firstname)
self.driver.find_element_by_id(self.manage_users_lastname).send_keys(manage_users_lastname)
self.driver.find_element_by_id(self.manage_users_email).send_keys(manage_users_email)
预期结果:
实际结果:
所需的元素是 动态元素 所以要 click()
在元素上你必须为 WebDriverWait =14=] 并且您可以使用以下任一项
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.expand i.fa.fa-plus-circle[id^='expand_']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='expand']//i[@class='fa fa-plus-circle' and starts-with(@id, 'expand_')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC