lundi 20 novembre 2017

Python run loop until keyword is found inside list selenium element

I have a list of elements they return like this

<selenium.webdriver.remote.webelement.WebElement (session="d3cf9b70-cdc1-11e7-8b3e-570f82e3aaae", element=":wdc:1511161807512")>
<selenium.webdriver.remote.webelement.WebElement (session="d3cf9b70-cdc1-11e7-8b3e-570f82e3aaae", element=":wdc:1511161807513")>
<selenium.webdriver.remote.webelement.WebElement (session="d3cf9b70-cdc1-11e7-8b3e-570f82e3aaae", element=":wdc:1511161807514")>
<selenium.webdriver.remote.webelement.WebElement (session="d3cf9b70-cdc1-11e7-8b3e-570f82e3aaae", element=":wdc:1511161807515")>

How I collect them

for comment in driver.find_elements_by_xpath("//*[contains(text(), '')]"):

Inside the elements there is text I can see the text like this

print(comment.text)

how do I run the loop until I have found a keyword inside of the elements list?

For example the keyword could be "LakerKobe". If it finds the element with the text of the keyword I set, I want the loop to stop running.

This is what I thought would work

for comment in driver.find_elements_by_xpath("//*[contains(text(), '')]"):
    if 'LakerKobe' in comment.text:
        print('found')

However this method is unreliable. It works & sometimes it doesn't. For example if you run this

keywordMain = 'if you shoot heroin 90% chance your dead already. herion is a different beast'

if 'heroin' in keywordMain:
        print('found')

you will see nothing is returned. I am using python 3.6

Aucun commentaire:

Enregistrer un commentaire