不知道用 Selenium 单击特定图标时要选择什么 Python
Don't know what to pick for clicking on specific icon with Selenium Python
我目前正在使用 Selenium 和 bs4 进行一个项目。到目前为止一切顺利,我可以毫无问题地使用 selenium 浏览网站,但是,我需要单击一个特定的图标来打开一个新选项卡,其中包含我需要 scrape 的一些信息使用BS4。我的问题是在查看 HTML 时,我不知道要用 Selenium 定位什么才能单击该图标。
HTML样本:
<a onmouseover="XYZ.Util.showHoverText(this, 'Print view')" href=
"app/exports/timesheet_print.php?startDate=2020-09-07&endDate=2020-09-11&filter_user=110483" target="_blank">
<img src="images/icons/printer.png" class="icon">
</a>
通常我会按名称、ID 或 Class 查找项目,但在那种情况下我不知道该从中选择什么。
我应该寻找 xpath 吗?
点击需要诱导的图标 for the element_to_be_clickable()
and you can use either of the following :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='app/exports/timesheet_print']>img.icon[src^='images/icons/printer']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'app/exports/timesheet_print')]/img[@class='icon' and starts-with(@src, 'images/icons/printer')]"))).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 和 bs4 进行一个项目。到目前为止一切顺利,我可以毫无问题地使用 selenium 浏览网站,但是,我需要单击一个特定的图标来打开一个新选项卡,其中包含我需要 scrape 的一些信息使用BS4。我的问题是在查看 HTML 时,我不知道要用 Selenium 定位什么才能单击该图标。
HTML样本:
<a onmouseover="XYZ.Util.showHoverText(this, 'Print view')" href=
"app/exports/timesheet_print.php?startDate=2020-09-07&endDate=2020-09-11&filter_user=110483" target="_blank">
<img src="images/icons/printer.png" class="icon">
</a>
通常我会按名称、ID 或 Class 查找项目,但在那种情况下我不知道该从中选择什么。
我应该寻找 xpath 吗?
点击需要诱导的图标element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='app/exports/timesheet_print']>img.icon[src^='images/icons/printer']"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'app/exports/timesheet_print')]/img[@class='icon' and starts-with(@src, 'images/icons/printer')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC