1

我在使用 Selenium WebDriver 查找元素时需要帮助。我已经尝试了所有可能的选择器,但我无法找到该元素。

供参考。id="ext-gen26" 不是常量,每次加载新页面时都会改变。

Tried Selectors:

    By.xpath("//button[@class=\"x-btn-text\"]/text()='Find Accounts'")
    By.cssSelector(input[name='id'])
    By.id("ext-gen26") 

Code:
    <div id="x-form-el-ext-gen26" class="x-form-element" style="padding-left:155px">
    <input id="ext-gen26" class="x-form-text x-form-field " type="text" name="id" 
    autocomplete="off" size="20" style="width: 212px;">
    </div>

我想定位 Account ID 元素并将文本发送到定位的元素。

欣赏,如果你可以请洒一些光。


在创建时调整自定义 UITableViewCell 的子视图

我正在尝试使用自己的 XIB 创建一个自定义 UITableViewCell,其中包含两个重要的可编辑组件:

  • UIL标签
  • UIImage with 'end-cap insets' - 此图像位于标签后面,并随其调整大小

当我创建我的 UITableViewCell 时,我可以轻松地设置其标签的文本,但在相同的方法中,我也尝试更改 UIImage 的框架以匹配 UILabel 的框架。但是,直到我通过滚动表格视图以将其出列并将其重新显示为“重新加载”单元格时,才会发生这种情况。

我在视图控制器的 .m 中创建自定义表格视图单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *UITableViewCellIdentifier = @"msgCell";

    RMMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:UITableViewCellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MessageCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
        [cell customInit]; //This sets up the ImageView's image etc
    }

    [cell setMessageValue:[_messages objectAtIndex:indexPath.row]]; //SEE BELOW

    return cell;
}

...在我的自定义单元格的 .m 中,我使用以下方法处理设置文本标签和调整图像大小:

- (void)setMessageValue:(NSString *)message {

    _testLabel.text = message;
    // Assume the text label's frame has already been set to the message's length 
    // (will add this in once I figure out how to change the imageView's 
    // frame correctly)

    // Set the image view's frame to the label's
    // THIS DOES NOT TAKE EFFECT UNTIL THE TABLE VIEW IS RE-SCROLLED
    CGRect frame = _imageView.frame;
    frame.size.width = _testLabel.frame.size.width + 8;
    _imageView.frame = frame;
}
4

3 回答 3

0

每次加载新页面时,div 的 id 是否也会发生变化?

我建议你写这样的东西:

css 选择器:"div.x-form-element input"

但是,如果您有很多带有x-form-elementasclass值的 div,则可以使用该nth-child()函数。喜欢 :

css 选择器:"div.x-form-element:nth-child(n) input" // n is the position order of your div that has x-form-element as class value.

nth-child() 文档

于 2013-04-28T20:02:50.627 回答
0

这是我的方法:在客户端使用 ExtJS 组件查询获取 ID,然后使用 ID 定位 WebElement。要发送密钥,您通常需要更深入地挖掘一层才能获得“inputEl”。Java 代码示例:

//a fully qualified ExtJS component query that will return one match only
String query = "viewport #panel1 textfield[fieldLabel='Test Field']";
//use component query to find id
String js = "return Ext.ComponentQuery.query(\"" + query + "\")[0].inputEl.id;";
String id = (String) ((JavascriptExecutor) _driver).executeScript(js);    
WebElement element = driver.findElement(By.id(id));
element.sendKeys("it works");
于 2013-10-11T00:16:04.037 回答
0

您的被测应用程序必须在输入字段、组合框、复选框等后面带有标签。

请尝试以下操作,考虑到您的应用程序的标签为“帐户 ID”

(//*[text()='Account ID']/following::input)[1]

HTML 代码将如下所示 -

<html>
<div id="x-form-el-ext-gen26" class="x-form-element" style="padding-left:155px">
    <label>Account ID: </label>
    <input id="ext-gen26" class="x-form-text x-form-field " type="text" name="id" 
    autocomplete="off" size="20" style="width: 212px;">
    </div>
</html>

除非标签发生变化,否则这种 xpath 永远不会失败。

于 2017-01-25T12:46:25.820 回答