seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.46k stars 909 forks source link

Highlight all element from sb.find_visible_elements() using for loop #2746

Closed adarmawan117 closed 2 months ago

adarmawan117 commented 2 months ago

I want to highlight all the element from sb.find_visible_elements(). This is my code

from seleniumbase import SB

with SB(uc=True, incognito=True) as sb:
    url = 'https://www.tokopedia.com/samsung/product'
    sb.open(url)

    sb.driver.sleep(3)
    elements = sb.find_visible_elements('//*[@data-testid="master-product-card"]')
    print(len(elements))
    for elem in elements:
        sb.highlight(elem, loops=6)
    sb.driver.sleep(3)

This code give me error

Traceback (most recent call last):
  File "D:\ADR\Programming\Program\MyProgram\Python\BOT_POSTING_MARKETPLACE\Gambar5_Website_JASA\bot_posting_marketplace_v1\scrape_tokopedia_store_product_lists.py", line 11, in <module>
    sb.highlight(elem, loops=6)
  File "D:\ADR\Programming\Program\MyProgram\Python\BOT_POSTING_MARKETPLACE\bot_posting_marketplace_v1\venv\Lib\site-packages\seleniumbase\fixtures\base_case.py", line 5745, in highlight
    self.wait_for_element_visible(selector, by=by, timeout=timeout)
  File "D:\ADR\Programming\Program\MyProgram\Python\BOT_POSTING_MARKETPLACE\bot_posting_marketplace_v1\venv\Lib\site-packages\seleniumbase\fixtures\base_case.py", line 8654, in wait_for_element_visible
    selector, by = self.__recalculate_selector(selector, by)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ADR\Programming\Program\MyProgram\Python\BOT_POSTING_MARKETPLACE\bot_posting_marketplace_v1\venv\Lib\site-packages\seleniumbase\fixtures\base_case.py", line 13366, in __recalculate_selector
    return page_utils.recalculate_selector(selector, by, xp_ok=xp_ok)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ADR\Programming\Program\MyProgram\Python\BOT_POSTING_MARKETPLACE\bot_posting_marketplace_v1\venv\Lib\site-packages\seleniumbase\fixtures\page_utils.py", line 92, in recalculate_selector
    raise Exception('Invalid selector type: "%s"\n%s' % (_type, msg))
Exception: Invalid selector type: "<class 'seleniumbase.undetected.webelement.WebElement'>"
Expecting a selector of type: "<class 'str'>" (string)!
mdmintz commented 2 months ago

Your problem code is here:

    for elem in elements:
        sb.highlight(elem, loops=6)

sb.highlight() expects a selector for the first argument. You passed a WebElement.

adarmawan117 commented 2 months ago

Yes, right. How can i ekstrak selector from that list.? I mean, for every element of that list.

So, i can highlight all the element.

mdmintz commented 2 months ago

In the next version of SeleniumBase, I'll update the sb.highlight() method so that a WebElement can also be passed.

adarmawan117 commented 2 months ago

Love it. Thanks Sir... ❤️

mdmintz commented 2 months ago

Updates made: https://github.com/seleniumbase/SeleniumBase/issues/2747 Upgrade to 4.26.3 (or newer if available). Don't forget to Star ⭐ SeleniumBase on GitHub: https://github.com/seleniumbase/SeleniumBase/stargazers

adarmawan117 commented 2 months ago

Really love it. I use this feature to change my code. And make it more interactive.

elements = sb.find_visible_elements('//*[@data-testid="master-product-card"]')
# sb.driver.execute_script("arguments[0].scrollIntoView();", elements[-1]) # CHANGE THIS CODE
sb.highlight(elements[-1])