Python元素单击
凭借我在selenium方面的初学者知识,我试图找到click元素,以打开链接。这些项目的链接没有href。如何单击正确的元素以打开链接 我正在使用python、selenium、chrome web驱动程序、BeautifulSoup。所有库都已更新 下面是示例html片段,其中有一个标题我需要使用selenium单击。请让我知道,如果你需要更多的html源代码。此代码来自仅“登录”的网站Python元素单击,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,凭借我在selenium方面的初学者知识,我试图找到click元素,以打开链接。这些项目的链接没有href。如何单击正确的元素以打开链接 我正在使用python、selenium、chrome web驱动程序、BeautifulSoup。所有库都已更新 下面是示例html片段,其中有一个标题我需要使用selenium单击。请让我知道,如果你需要更多的html源代码。此代码来自仅“登录”的网站 <h2> <!--For Verified item--> <a cl
<h2> <!--For Verified item-->
<a class="clickable" style="cursor:pointer;" onmousedown="open_item_detail('0000013', '0', false)" id="View item Detail" serial="0000013">
Sample Item
</a>
<!--For unverified item-->
</h2>
样品
等待元素,然后通过正确的xpath进行查找
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://yourpage.com")
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//contains(a[text(),"Sample Item")]')))
elem.click()
等待元素,然后通过正确的xpath查找
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://yourpage.com")
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//contains(a[text(),"Sample Item")]')))
elem.click()
要单击元素,您需要使
元素成为可单击的()
,您可以使用以下任一选项:
- 使用部分链接文本:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sample Item"))).click()
- 使用
:CSS\u选择器
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h2 a.clickable[onmousedown^='open_item_detail']"))).click()
- 使用
:XPATH
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2//a[@class='clickable' and starts-with(@onmousedown, 'open_item_detail')][contains(., 'Sample Item')]"))).click()
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
元素可单击()
,您可以使用以下任一选项:
- 使用部分链接文本:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sample Item"))).click()
- 使用
:CSS\u选择器
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h2 a.clickable[onmousedown^='open_item_detail']"))).click()
- 使用
:XPATH
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2//a[@class='clickable' and starts-with(@onmousedown, 'open_item_detail')][contains(., 'Sample Item')]"))).click()
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC