简体   繁体   中英

How can I find elements containing a specific string by xpath in php?

for an automatic test I want checkboxes of the rows containing a specific string to be checked. when I search elements by xpath in the console, it finds exactly the elements I want, but when I run the tests it checks all the checboxes, no matter if they contain that string.

I think the problem lies in the function:

public function iCheckCheckboxInRowsWith($col){

    $checkboxes = $this->getSession()->getPage()->findAll(
selector:'xpath',    
    sprintf('//table/tbody/tr[td[contains(text(), %s)]]/td/input[@type="checkbox"]',
    $col));    
    dump($checkboxes);    
    foreach( $checkboxes as $checkbox){    
        $checkbox->click();
    }

}

In terminal I see that the array $checkboxes (which I have dumped) consists of all checkboxes. How can I fix it?

try with following XPath. It may works.

sprintf('//table/tbody/tr/td[contains(text(), %s)]/input[@type="checkbox"]', $col)

or try with following

sprintf('//table/tbody/tr/td[contains(., %s)]/input[@type="checkbox"]', $col)

您可以使用:

sprintf("//table/tbody/tr/td[contains(., '" +col+ "')]/input[@type='checkbox']"));

尽管我在注释中声明 $col 是一个字符串,但我仍然必须将 %s 放在引号中,因此:

//table/tbody/tr/td[contains(text(), "%s")]/preceding::td[1]/input[@type="checkbox"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM