使用类' x'查找元素,忽略具有类' x'和另一个班

时间:2015-12-02 15:30:37

标签: javascript html selenium selenium-webdriver

我正在使用Selenuim和Javascript来测试我的测试。 DOM有4个元素:

<input class="hidden-element xyz"><input class="hidden-element xyz"><input class="xyz"><input class="xyz">

我希望找到仅包含xyz类的元素,而不是包含hidden-elementxyz的元素。当我说:

if (driver.findElements({css: 'section[title="custom"] > input.xyz'})) {
  driver.findElements({css: 'section[title="custom"] > input.xyz'}).then(function(inputs) {
    for (i = 0; i < inputs.length; i++) {
      inputs[i].sendKeys('Input Text');
    }
  });
}

它将拉出所有4个元素,尝试填充隐藏元素,并打破测试。我试过了:

if (driver.findElements({css: 'section[title="custom"] > input.xyz'}) && !driver.findElements({css: 'section[title="custom"] > input.hidden-element'})) {
  driver.findElements({css: 'section[title="custom"] > input.xyz'}).then(function(inputs) {
    for (i = 0; i < inputs.length; i++) {
      inputs[i].sendKeys('Input Text');
    }
  });
}

但这也不起作用。我也一直在玩这种不同的变化,但无济于事。这似乎很简单,但我很难在解决方案上磨练。想法?

2 个答案:

答案 0 :(得分:3)

你的选择器应该是:

.xyz:not(.hidden-elem)

这是因为A and not(A and B)有效地倾注到A and not B

编辑:好像你已经找到了逻辑

答案 1 :(得分:1)

我建议,如果可能有多个其他类名要过滤掉:

function uniquelyClassed (arr, cName) {
  if (arr instanceof Array) {
  return arr.filter(function (n) {
    return n.classList.length === 1 && n.classList.contains(cName);
  });
  }
}

var elements = document.querySelectorAll('input'),
    elementsArray = Array.prototype.slice.call(elements, 0),
    uniquelyXYZ = uniquelyClassed(elementsArray, 'xyz');

uniquelyXYZ.forEach(function (el) {
  el.classList.add('found');
});

&#13;
&#13;
input {
  margin: 0 0 0.5em 0;
  display: block;
}
.found {
  border-color: limegreen;
}
&#13;
<input class="hidden-element xyz">
<input class="hidden-element xyz">
<input class="xyz">
<input class="xyz">
&#13;
#! /usr/bin/python

#Install requests package for python
import requests
import csv, gzip, sys


# Set the request parameters
url = 'https://xxxxxxxxdev.service-now.com/api/now/table/new_call'
user = 'xxxxx'
pwd = 'xxxxxx'

event_count = int(sys.argv[1])  # number of events returned.
results_file = sys.argv[8]      # file with search results

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

for row in csv.DictReader(openany(results_file)):
output="{"
for name,val in row.iteritems():
   if output!="{":
           output+=","
    output += '"'+name+'":"'+val+'"'
output+="}"

# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers,     data='{"short_description":"Theo\'s Test for Splunk to SN","company":"company\'s      domain","u_source":"Service Desk","contact_type":"Alert","description":"Please     place detailed alert detail including recommended steps"}')

# Check for HTTP codes other than 200
if response.status_code != 201:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error       Response:',response.json())
exit()

# Decode the JSON response into a dictionary and use the data
#resp=response.json()
 #print('Status:',response.status_code,'Headers:',response.headers,'Response:',re    sponse.json())
print response.headers['location']
}
&#13;
&#13;
&#13;

JS Fiddle demo

参考文献: