Python Selenium:单击div中的链接
我正在学习selenium,我正在尝试一件简单的事情:点击维基百科上搜索的第一个结果Python Selenium:单击div中的链接,python,selenium,web-scraping,Python,Selenium,Web Scraping,我正在学习selenium,我正在尝试一件简单的事情:点击维基百科上搜索的第一个结果 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.supp
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://en.wikipedia.org")
# Input in the search bar
search = driver.find_element_by_id("searchInput")
search.send_keys("python", Keys.RETURN)
wait = WebDriverWait(driver, 10)
first_result = wait.until(
EC.presence_of_element_located((By.CLASS_NAME, "mw-search-result-heading"))
)
first_result.click()
所以我可以选择第一个结果的标题。但我不能点击它。
我想这是因为我点击的是包含链接的
,而不是实际的链接。以下是HTML的一个片段:
如果实际的
链接没有任何类名、id或名称,我如何定位该链接?因为您使用的是Selenium,所以可能需要使用XPath
。这帮我节省了很多时间!只需右键单击所需的标记,然后单击复制xpath。
然后使用与此处类似的语法单击对象:
elem = driver.find_element_by_xpath('//some_xpath')
elem.click()
因为您使用的是Selenium,所以可能需要使用
XPath
。这帮我节省了很多时间!只需右键单击所需的标记,然后单击复制xpath。
然后使用与此处类似的语法单击对象:
elem = driver.find_element_by_xpath('//some_xpath')
elem.click()
您可以链接
find.*
函数,以获得更具体的WebElement查询:
first_result.find_element_by_tag_name("a").click()
您可以链接
find.*
函数,以获得更具体的WebElement查询:
first_result.find_element_by_tag_name("a").click()
点击link
Python(langage)
includeWebDriverWait
(),等待元素可点击
(),然后使用css选择器
或xpath
或链接文本
CSS选择器:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
Xpath:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
链接\u文本:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
点击link
Python(langage)
includeWebDriverWait
(),等待元素可点击
(),然后使用css选择器
或xpath
或链接文本
CSS选择器:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
Xpath:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
链接\u文本:
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Python (langage)']")))
first_result.click()
driver.get("https://en.wikipedia.org")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"searchInput"))).send_keys("python", Keys.RETURN)
first_result = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Python (langage)")))
first_result.click()
元素的存在\u位于不返回元素-它只返回true`或false
-即,是否找到find\u element\u by…
方法可用于return
elements.reference for basics-@Haezer哪个元素by.CLASS\u NAME,“mw搜索结果标题”
您正在尝试定位?所定位元素的存在不返回元素-它只返回true`或false
-即,是否找到find\u element\u by…
方法可用于return
elements.reference for basics-@haizer该元素by.CLASS\u NAME,“mw搜索结果标题”
您试图查找的元素?感谢您提供的提示,它肯定会在将来非常有用!它并没有回答最初的问题,因为链接的xpath对于每个搜索似乎都是不同的。谢谢你的提示,相信它在将来会非常有用!它并没有回答最初的问题,因为链接的xpath对于每个搜索似乎都是不同的。