Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How do I determine text visibility in multiple elements #1431

Answered by mdmintz
chenqinggang001 asked this question in Q&A
Discussion options

This element has more than one on the page, and I try using 'self.is_text_visible', which will only look for the first element:

image

Does seleniumBase provide methods for that?

I have two needs:

  1. Find if the element containing the text exists

  2. Locate the element by text

You must be logged in to vote

Use the TAG:contains(TEXT) selector for selectors containing text. Example: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_contains_selector.py

from seleniumbase import BaseCase
class ContainsSelectorTests(BaseCase):
 def test_contains_selector(self):
 self.open("https://xkcd.com/2207/")
 self.assert_element('div.box div:contains("Math Work")')
 self.click('a:contains("Next")')
 self.assert_element('div div:contains("Drone Fishing")')

More examples here: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_tinymce.py

from seleniumbase import BaseCase
class TinyMceTests(BaseCase):
 def test_tinymce(self):
 s...

Replies: 3 comments

Comment options

I pass self. find_elements implements the function

But I still want to judge text visibility in multiple elements

You must be logged in to vote
0 replies
Comment options

Use the TAG:contains(TEXT) selector for selectors containing text. Example: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_contains_selector.py

from seleniumbase import BaseCase
class ContainsSelectorTests(BaseCase):
 def test_contains_selector(self):
 self.open("https://xkcd.com/2207/")
 self.assert_element('div.box div:contains("Math Work")')
 self.click('a:contains("Next")')
 self.assert_element('div div:contains("Drone Fishing")')

More examples here: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_tinymce.py

from seleniumbase import BaseCase
class TinyMceTests(BaseCase):
 def test_tinymce(self):
 self.open("https://seleniumbase.io/tinymce/")
 self.wait_for_element("div.mce-container-body")
 self.click('span:contains("File")')
 self.click('span:contains("New document")')
 self.click('span:contains("Paragraph")')
 self.click('span:contains("Heading 2")')
 self.switch_to_frame("iframe")
 self.add_text("#tinymce", "Automate anything with SeleniumBase!\n")
 self.switch_to_default_content()
 self.click("button i.mce-i-image")
 self.type('input[aria-label="Width"].mce-textbox', "300")
 image_url = "https://seleniumbase.io/img/sb_logo_10.png"
 self.type("input.mce-textbox", image_url + "\n")
 self.switch_to_frame("iframe")
 self.click("h2")
 self.switch_to_default_content()
 self.post_message("Automate anything with SeleniumBase!")
 self.click('span:contains("File")')
 self.click('span:contains("Preview")')
 self.switch_to_frame('iframe[sandbox="allow-scripts"]')
 self.post_message("Learn SeleniumBase Today!")

You can use it with methods such as:
is_visible = self.is_element_visible('div:contains("TEXT")')

You must be logged in to vote
0 replies
Answer selected by mdmintz
Comment options

Thank you

I did it the following way:

self.wait_for_element("//table//tr")
if self.is_element_visible(f'span:contains("{now_time}")'):
 self.click(f'span:contains("{now_time}")')
else:
 ...
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /