我试图找到一个包含子字符串'.ics'的href,例如在屏幕截图中,并将链接作为字符串返回。
以下是我的代码尝试:
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get('http://miamioh.edu/emss/offices/career-services/events/index.html') element = driver.find_element_by_partial_link_text('.ics')但是,我收到此错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":".ics"}毫无疑问,我忽略了一些非常基本的东西,但我无法弄清楚是什么。 我也试过这条线
element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();而不是以'element'开头的另一行。 但是,这给了
AttributeError: 'WebDriver' object has no attribute 'findElement'I am trying to locate an href containing the substring '.ics', such as in the screenshot, and return the link as a string.
Below is my code attempt:
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get('http://miamioh.edu/emss/offices/career-services/events/index.html') element = driver.find_element_by_partial_link_text('.ics')However, I get this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":".ics"}No doubt I am overlooking something very basic, but I can't figure out what. I have also tried the line
element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();instead of the other line beginning with 'element'. However, this gives
AttributeError: 'WebDriver' object has no attribute 'findElement'最满意答案
链接文本是您在网页上看到的链接的精确文本,而部分链接文本只是该链接文本的某些子字符串。
"services.ics"是href属性的一部分。 如果你想通过"services.ics"找到元素,你可以使用
driver.find_element_by_xpath('//a[contains(@href, "services.ics")]')您也可以使用title属性来匹配所需的元素:
driver.find_element_by_xpath('//a[@title="iCal Feed"]')注意
element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();是Python代码的Java模拟
from selenium.webdriver.common.by import By element = driver.find_element(By.CSS_SELECTOR, "a[href*='services.ics']").click();更新
链接可能是动态生成的,因此您可以尝试按如下方式应用ExplicitWait以避免NoSuchElementException :
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait as wait wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[@title="iCal Feed"]'))).click()更新2
作为位于iframe内的目标链接,您应该在单击链接之前切换到该框架:
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("trumba.spud.1.iframe")) driver.find_element_by_xpath('//a[@title="iCal Feed"]').click()The link text is exact text of the link you see on web page while partial link text is just some substring of that link text.
"services.ics" is part of href attribute. If you want to find element by "services.ics" you might use
driver.find_element_by_xpath('//a[contains(@href, "services.ics")]')Also you might use title attribute to match required element:
driver.find_element_by_xpath('//a[@title="iCal Feed"]')Note that
element = driver.findElement(By.cssSelector("a[href*='services.ics']")).click();is Java analogue of Python code
from selenium.webdriver.common.by import By element = driver.find_element(By.CSS_SELECTOR, "a[href*='services.ics']").click();Update
Link might be generated dynamically, so you can try to apply ExplicitWait as below to avoid NoSuchElementException:
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait as wait wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[@title="iCal Feed"]'))).click()Update 2
As target link located inside an iframe you should switch to that frame before clicking link:
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("trumba.spud.1.iframe")) driver.find_element_by_xpath('//a[@title="iCal Feed"]').click()更多推荐
发布评论