1

I'm trying to do some basic stuff with Selenium and I'm trying to find an element by the "Text" since all my elements are either dynamically generated or they have a common attribute name and value.

i would like to get to "SelTest" at the following div, here is one block:

 <div ng-mouseenter="hover=true" ng-mouseleave="hover=false" class="object workspace" ng-repeat="workspace in workspaces | filter:{'workspace_title':workspaceFilter, 'is_archived':false} | orderBy: 'workspace_title'" ui-sref="main.workspace({workspaceId: workspace.id})" href="#!/workspace/a43d95fa24acbfc4757ba86a4d8dec22">
            <i class="mdi mdi-folder"></i>
            <div class="title">SelTest</div>
            <div class="desc"></div>
            <div class="tools ng-hide" ng-show="hover" style="">
                <button type="button" ng-click="workspace.toggleArchive();$event.stopPropagation();" class="btn btn-default btn-xs">
                    <span>Archive</span>
                </button>
            </div>
        </div>

while the rest of the code have the same exact attribute name and value.

and here is the bigger picture:

enter image description here

Is there anyway to get to SelTest?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
CodeMonkey
  • 2,511
  • 4
  • 27
  • 38

1 Answers1

1

To locate the element with text as SelTest you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebElement ele = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.object.workspace[ng-repeat^='workspace in workspaces'][href*='workspace'] div.title")));
    
  • xpath:

    WebElement ele = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='object workspace' and starts-with(@ng-repeat, 'workspace in workspaces')][contains(@href, 'workspace')]//div[@class='title' and text()='SelTest']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I had no idea about ExpectedConditions which is great and that xpath THOUGH! thank you very much. your answer made me realize I have to go through my codes again. thank you – CodeMonkey Apr 16 '19 at 11:29