如何在xpath中处理单引号

时间:2016-07-12 10:06:01

标签: python selenium xpath

我有一行,用于检查页面上是否存在部分文字的特定元素。

self.b.find_element_by_xpath(".//*[contains(text(), '%s')]" % item_text)

因此item_text可能在字符串中有单引号。例如"Hanes Men's Graphic "

变为self.b.find_element_by_xpath(".//*[contains(text(), 'Hanes Men's Graphic ')]")

在这种情况下,我收到错误:

InvalidSelectorError: Unable to locate an element with the xpath expression .//*[contains(text(), 'Hanes Men's Graphic ')] because of the following error:
SyntaxError: The expression is not a legal expression.

item_text

中使用self.b.find_element_by_xpath(".//*[contains(text(), '%s')]" % item_text)之前,我应该对-(void)parseJSON:(NSArray *)json entity:(NSString *)entityName{ [json enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSArray *primaryKeyNameJSON = [self getPrimaryKeyNameForEntityInJSON:entityName]; NSArray *primaryKey = [obj objectsForKeys:primaryKeyNameJSON notFoundMarker:@""]; NSArray *primaryKeyName = [self getPrimaryKeyNameForEntity:entityName]; NSDictionary *primaryKeysAndValues = [NSDictionary dictionaryWithObjects:primaryKey forKeys:primaryKeyName]; NSManagedObject *managedObj = [self getManagedObjectForEntity:entityName primaryKey:primaryKeysAndValues jsonEntity:entityName json:obj]; NSDictionary *allAttributes = [[managedObj entity] attributesByName]; NSArray *attributesKeys = [allAttributes allKeys]; NSDictionary *allRelationships = [[managedObj entity] relationshipsByName]; NSArray *relationshipsKeys = [allRelationships allKeys]; NSArray *keys = [obj allKeys]; for (NSString *jsonKey in keys) { NSString *dbKey = [self getDBKeyForJSONKey:jsonKey entity:entityName]; if ([attributesKeys containsObject:dbKey]) { //Attribute NSAttributeDescription *oneAttribute = [allAttributes objectForKey:dbKey]; if ([oneAttribute attributeType] == NSDateAttributeType){ [managedObj setValue:[[self dateFormatterJSon] dateFromString:[obj objectForKey:jsonKey]] forKey:dbKey]; }else if ([oneAttribute attributeType] == NSInteger32AttributeType || [oneAttribute attributeType] == NSFloatAttributeType){ [managedObj setValue:[[self numberFormatterJSon] numberFromString:[obj objectForKey:jsonKey]] forKey:dbKey]; }else if ([oneAttribute attributeType] == NSStringAttributeType) [managedObj setValue:[obj objectForKey:jsonKey] forKey:dbKey]; else if ([oneAttribute attributeType] == NSBooleanAttributeType) { if ([[obj objectForKey:jsonKey] isEqual: @"-1"] || [[obj objectForKey:jsonKey] isEqual: @"1"]){ [managedObj setValue:[NSNumber numberWithBool:YES] forKey:dbKey]; }else{ [managedObj setValue:[NSNumber numberWithBool:NO] forKey:dbKey]; } } } else if ([relationshipsKeys containsObject:dbKey]) { //Relationship NSRelationshipDescription *oneRelationship = [allRelationships objectForKey:dbKey]; NSString *destRelationshipEntityName = [[oneRelationship destinationEntity] name]; NSArray *destRelationshipPrimaryKeyName = [self getPrimaryKeyNameForEntity:destRelationshipEntityName]; NSArray *destRelationshipPrimaryKeyValues = [[NSArray alloc] init]; destRelationshipPrimaryKeyValues = [obj objectsForKeys:[self getPrimaryKeyNameForEntity:destRelationshipEntityName inJSONEntity:entityName attrName:jsonKey] notFoundMarker:@""]; NSDictionary *destRelPrimaryKeysAndValues = [NSDictionary dictionaryWithObjects:destRelationshipPrimaryKeyValues forKeys:destRelationshipPrimaryKeyName]; NSManagedObject *destManagedObject = [self getManagedObjectForEntity:destRelationshipEntityName primaryKey:destRelPrimaryKeysAndValues jsonEntity:entityName json:obj]; [managedObj setValue:destManagedObject forKey:dbKey]; } }]; } 执行哪些操作

我知道我可以在表达式周围使用单引号并在内部使用双引号,但这不是我正在寻找的解决方案。

2 个答案:

答案 0 :(得分:4)

尝试如下: -

self.b.find_element_by_xpath(".//*[contains(text(), \"Hanes Men's Graphic\")]")

self.b.find_element_by_xpath('.//*[contains(text(), "%s")]' % item_text)

self.b.find_element_by_xpath(".//*[contains(text(), \"%s\")]" % item_text)

希望它能起作用.. :)

答案 1 :(得分:0)

可能是xpath实现不允许字符串使用双引号,因此您需要执行类似

的操作
if (item_text.find("'")):
    item_text= item_text.replace("'", "', \"'\", '")
    item_text= "concat('" + item_text+ "')"
else:
    item_text = "'" + item_text + "'"

self.b.find_element_by_xpath(".//*[contains(text(), %s)]" % item_text)