例如,我有这样的HTML代码。我想通过“ T恤名称”和“颜色-T恤”找到它。这该怎么做?我想找到这种颜色,因为我还有其他一些名字相同但颜色不同的T恤。
<article>
<div class="article">
<a style="height:150px;" href="/shop/t-shirt">
<img src="//blablabla.jpg" alt="asdasdas" width="150" height="150">
<h1><a class="name-link" href="/shop/t-shirt/">T-shirt name</a>
</h1>
<p><a class="name-link" href="/shop/t-shirt/">>Color</a></p>
</div>
</article>
<article>
<div class="article">
<a style="height:150px;" href="/shop/t-shirt">
<img src="//blablabla.jpg" alt="asdasdas" width="150" height="150">
<h1><a class="name-link" href="/shop/t-shirt/">T-shirt name</a>
</h1>
<p><a class="name-link" href="/shop/t-shirt/">>second Color</a></p>
</div>
</article>
答案 0 :(得分:1)
使用WebDriverWait
和visibility_of_all_elements_located
并遵循xpath
来实现。
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//h1/a[@class='name-link' and contains(.,'T-shirt name')]")))
for element in elements :
print('T shirt name : ' +element.text)
print('T Shirt collor : ' + element.find_element_by_xpath("./following::p/a[@class='name-link']").text)
您需要导入以下内容以执行上述代码。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
使用特定颜色的单击选项进行编辑。
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//h1/a[@class='name-link' and contains(.,'T-shirt name')]")))
for element in elements :
print('T shirt name : ' +element.text)
print('T Shirt collor : ' + element.find_element_by_xpath("./following::p/a[@class='name-link']").text)
if element.find_element_by_xpath("./following::p/a[@class='name-link']").text=='RED':
element.find_element_by_xpath("./following::p/a[@class='name-link']").click()
答案 1 :(得分:0)
您可以使用BeautifulSoup进行解析
soup.find_all("a", string="T-shirt name")