Select an item with text using Selenium WebDriver

I use Selenium WebDriver and Python bindings to automate some of the same tedious WordPress tasks, and until that point, it was pretty simple. I am trying to select a check box, but the only way I can identify it is with the text following it. Here is the relevant part of the HTML:

<li id="product_cat-52"> <label class="selectit"> <input value="52" type="checkbox" name="tax_input[product_cat][]" id="in-product_cat-52"> polishpottery </label> </li> 

The only information I have in my script to identify this flag is the string "polishpottery". Is there a way to select this checkbox, knowing only the text that follows?

+6
source share
3 answers

As already mentioned in sherwin-wu, you should find a way to choose what you want based on the identifier or name or class (and most likely, a combination of it). In your example, there seems to be enough scope for this, although I don't know what the rest of the page looks like.

Having said that, you can do what you asked to use the XPath selector, for example

 driver.find_element_by_xpath("//li/label/input[contains(..,'polishpottery')]") 
+8
source

Regular expressions may not be the best solution, but it should work.

 import re def get_id(str, html_page): # str in this case would be 'polishpottery' return re.search(r'<input[^<>]*?type="checkbox"[^<>]*?id="([A-Za-z0-9_ -]*?)"[^<>]*?> ?' + str, html_page).group(1) id = get_id('polishpottery', html) checkbox = driver.find_element_by_id(id) checkbox.toggle() # Or, more minimallistically: driver.find_element_by_id(get_id('polishpottery', html)).toggle() 

Conclusion:

 >>> print(html) <li id="product_cat-52"> <label class="selectit"> <input value="52" type="checkbox" name="tax_input[product_cat][]" id="in-product_cat-52"> polishpottery </label> </li> >>> get_id('polishpottery', html) 'in-product_cat-52' 
0
source

I would recommend trying to find other ways to select the checkbox. For example, you can select the li tag based on its identifier using browser.find_element_by_id (id). You can also select based on the name using browser.find_element_by_name (name).

Alternatively, if you really cannot, you can select text using selenium + BeautifulSoup.

 soup = BeautifulSoup(browser.page_source) text = soup.find('input', re.compile=" polishpottery") checkbox = text.parent # it might not exactly be parent, but you can play around with # navigating the tree. 

Hope this helps!

0
source

Source: https://habr.com/ru/post/920095/


All Articles