How to search a webpage using selenium by any of the keywords inputted?

我怕爱的太早我们不能终老 提交于 2021-02-17 03:25:31

问题


I am new to selenium in python. I am searching the supreme website by keywords using Xpath as such:

WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
    (By.XPATH, "//a[contains(text(),'Supreme Time Tee')]"))).click()    

My question is how would I search keywords for any text and skip until it finds it? For example, I struggle to grab the t shirt labeled "Dragon Tee Heather Grey" that is split into two a href link tags "Dragon Tee" and "Heather Grey".

I want to be able to click either element but I am not sure how to construct this.


回答1:


can you try this xpath:

 //a[contains(text(),'Dragon Tee')] /parent::div/following-sibling::*/a[contains(text(),'Dusty')]



回答2:


You can use XPath to solve this by looking for sibling tags. For example, if you want to find the Dragon Tee Heather Grey link, you can use this.

driver.find_element_by_xpath('//div[a/text() = "Dragon Tee"]/following-sibling::div[a/text() = "Heather Grey"]')


来源:https://stackoverflow.com/questions/61354961/how-to-search-a-webpage-using-selenium-by-any-of-the-keywords-inputted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!