Webdriver.io如何根据页面上相似元素的数量选择和更改元素

时间:2018-06-22 05:40:21

标签: css selenium-webdriver xpath css-selectors webdriver-io

我想要一种方法来选择此named-source-add div内的udp-address-input字段。我遇到的问题是udp-address-input-<+ id>元素根据已添加的命名资源的数量而有所不同。命名源添加div内始终只有1个udp-address-input-<+ id>,所以我想知道是否可以将搜索限制在该div内?

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from django.views.generic.edit import FormView

from .models import Location, Hashtag
from .forms import SearchHashtagForm


class HashtagSearch(FormView):
    """ FormView for user to enter hashtag search query """

    template_name = 'mapping_twitter/hashtag_search_form.html'
    form_class = SearchHashtagForm

    def get_success_url(self):
        return reverse('mapping_twitter:results')

    def form_valid(self, form):
        form.clean_hashtag()
        form.save()
        return super().form_valid(form)

    def form_invalid(self, form):
        search_text = form.cleaned_data['search_text']
        if Hashtag.objects.filter(pk=search_text).exists():
            return HttpResponseRedirect(reverse('mapping_twitter:results'))
        else:
            return self.form_invalid(form)


class SearchResultsView(generic.ListView):
    """ Generic class-based view listing search results of locations """
    model = Hashtag
    template_name = 'mapping_twitter/results.html'

    def get_queryset(self, **kwargs):
        qs = super().get_queryset()
        return qs

    def get_context_data(self, **kwargs):
        context = super(SearchResultsView, self).get_context_data(**kwargs)
        context['search_text'] = Hashtag.objects.all()
        return context

1 个答案:

答案 0 :(得分:0)

我使用xpath,仅在named-source-add div内查找以udp-address-input开头的id的输入。

//div[@id='named-source-add']//input[contains(@id,'udp-address-input')]

我必须添加斜杠才能使其与我的javascript webdriverio pageobject一起使用,如下所示:

udpMulticastAddr:{ get() { return '//div[@id=\'named-source-add\']//input[contains(@id,\'udp-address-input\')]';}},