Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adarmawan117 opened this issue May 3, 2024 · 6 comments
Closed
Labels
invalid usage You may need to change what you're doing

Comments

@adarmawan117
Copy link

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 mdmintz added the invalid usage You may need to change what you're doing label May 3, 2024
@mdmintz
Copy link
Member

mdmintz commented May 3, 2024

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.

@mdmintz mdmintz closed this as completed May 3, 2024
@adarmawan117
Copy link
Author

adarmawan117 commented May 3, 2024

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
Copy link
Member

mdmintz commented May 3, 2024

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

@adarmawan117
Copy link
Author

adarmawan117 commented May 3, 2024

Love it. Thanks Sir... ❤️

@mdmintz
Copy link
Member

mdmintz commented May 3, 2024

Updates made: #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
Copy link
Author

adarmawan117 commented May 4, 2024

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])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid usage You may need to change what you're doing
Projects
None yet
Development

No branches or pull requests

2 participants