Selenium - 按 h1 和 p 文本查找文章
Selenium - find article by h1 and p text
如何通过 h1 和 p 文本在网站上查找文章,如下图所示?
我试过了,在那里我可以找到所有文章,但我不知道如何找到 h1 中的文本和 p 中的文本。然后我想点击这个。
text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')]/h1")
text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')][h1/a[contains(text(),"Beanie")]][p/a[contains(text(),"Red")]]")
您可以使用上面的 xpath,它将检查父元素 article/div 是否有子元素 h1/a 和 p/a 分别带有文本 Beanie 和 Red
在 w3chool 中 html 编辑器位于 iframe 中,因此在尝试查找元素之前,请在您的 seelnium 测试中切换到 iframe
要提取和打印文本 Beanie Custom First 和 Red,您需要引入 for the visibility_of_element_located()
and you can use either of the following :
使用 CSS_SELECTOR
和 text 属性:
印花首先定制帽子:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article h1 > a.name-link[href='/shop/asd']"))).text)
打印红色:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article p > a.name-link[href='/shop/asd']"))).text)
使用 XPATH
和 get_attribute()
:
印花首先定制帽子:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//h1/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
打印红色:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//p/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in
结尾
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium
如何通过 h1 和 p 文本在网站上查找文章,如下图所示?
我试过了,在那里我可以找到所有文章,但我不知道如何找到 h1 中的文本和 p 中的文本。然后我想点击这个。
text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')]/h1")
text = driver.find_elements_by_xpath("//article/div[contains(@class,'inner-article')][h1/a[contains(text(),"Beanie")]][p/a[contains(text(),"Red")]]")
您可以使用上面的 xpath,它将检查父元素 article/div 是否有子元素 h1/a 和 p/a 分别带有文本 Beanie 和 Red
在 w3chool 中 html 编辑器位于 iframe 中,因此在尝试查找元素之前,请在您的 seelnium 测试中切换到 iframe
要提取和打印文本 Beanie Custom First 和 Red,您需要引入 visibility_of_element_located()
and you can use either of the following
使用
CSS_SELECTOR
和 text 属性:印花首先定制帽子:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article h1 > a.name-link[href='/shop/asd']"))).text)
打印红色:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article p > a.name-link[href='/shop/asd']"))).text)
使用
XPATH
和get_attribute()
:印花首先定制帽子:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//h1/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
打印红色:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[@class='inner-article']//p/a[@class='name-link' and @href='/shop/asd']"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in
结尾
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium