例如,我有这样的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>
使用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()
好的工作,现在如何单击例如T Shirt collor ==“ Red”?
使用If条件检查第二个打印值,然后单击。我现在不在,我稍后再回来
@ konto211541:检查“编辑”部分