无法使用selenium-webdriver中的gettext提取文本,也无法单击它

时间:2013-05-23 03:25:58

标签: java selenium-webdriver

我无法在硒gettext中找到以下代码的webdriver

<a id="551" class="blueTextNormal1 spc" onclick="sPh(this,'079');return false;" title="079">Country</a>

我想获得Country的价值。我尝试使用xpath

driver.findElement(By.xpath("//*[@id='551']").getText())

但它没有返回任何值。当我尝试

driver.findElement(By.xpath("//*[@id='551']")).getAttribute("title"))

我得到的值为“079”

任何人都可以建议如何继续。

9 个答案:

答案 0 :(得分:18)

这也取决于代码。 尝试下面的代码: 请使用.getAttribute(“innerHTML”)代替.getAxt(),然后返回您要查找的内容,包括任何不可见的HTML

<div class="no-docs-selected">
<p class="icon-alert">Please select an doc</p>
</div>

我一直在寻找“请选择一个文档,但没有用.gettext()取得成功。但是下面有一个工作。

driver.findElement(By.xpath( “// P [@类= '图标警报']”))的getAttribute( “innerHTML的”);

答案 1 :(得分:9)

我遇到了同样的问题,然后我将.getText()更新为 .getAttribute(“textContent”),并且它有效。

答案 2 :(得分:0)

令人惊讶的是,你能够获得属性标题而不是文本,

你可以尝试一下吗

driver.findelement(By.xpath("//a[@id='551' and contains(text(),'Country')]")).isDisplayed();

driver.findelement(By.xpath("//a[@id='551' and text()='Country']")).isDisplayed();

答案 3 :(得分:0)

除非它是复制到stackoverflow时的类型,否则在getText之前缺少括号。更改 driver.findElement(By.xpath("//*[@id='551']").getText())driver.findElement(By.xpath("//*[@id='551']")).getText()

我尝试了以下代码,它对我来说非常合适。

WebDriver driver = new ChromeDriver();
driver.get("C:\\test.html");
System.out.println(driver.findElement(By.xpath("//*[@id='551']")).getText());
System.out.println(driver.findElement(By.xpath("//*@id='551']")).getAttribute("title"));

答案 4 :(得分:0)

使用.getAttribute(“value”)来提取文本

答案 5 :(得分:0)

       String text1=driver.findElementByXPath("//input[@value='EFASTGTC']/../ancestor::tr[1]/scipt[1]").getAttribute("innerText"); 

的System.out.println(&#34;值=&#34 +的text1);

&#34;如果您希望在脚本标记&#34;

之间编写文本,此代码将起作用

答案 6 :(得分:0)

我也遇到过gevent.queue.JoinableQueue有效但getAttribute("innerHTML")不起作用的情况。事实证明该元素存在但不可见或不显示。当我在调用getText()之前先滚动到该元素时,它起作用了。

getText()

if(!element.isDisplayed()) scrollIntoView(element); element.getText(); 函数如下:

scrollIntoView

因此,我不需要使用 public void scrollIntoView(WebElement element) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); }

答案 7 :(得分:0)

这对我有用:

          function getdata(Request $request)
{
    if(request()->ajax())
    {
        return datatables()->of(Casting::latest()->get())
            ->addColumn('action', function($data){
                $button = '<table><tr><td>';
                $button .= '<button type="button" name="edit" id="'.$data->id.'" class="edit btn btn-primary btn-sm">Modifier</button>';
                $button .= '</td><td>';
                 $button .= ' <div class="custom-control custom-switch">';
                $button .= ' <input type="checkbox" class="custom-control-input" id="switch1" name="example"';
                
                if ($data->status == 1) {

                    $button .= "checked";
                }

                $button .= '><label class="custom-control-label" for="switch1">Toggle me</label>
    </div>';
                $button .= '</td></tr></table>';
                return $button;
        })
        ->rawColumns(['action'])
        ->make(true);
    }
    return view('Casting.castingss');
}

代码:

System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));

答案 8 :(得分:-1)

在类似情况下我的工作:     .getAttribute(&#34; innerHTML的&#34);     .getAttribute(&#34;的innerText&#34);     .getAttribute(&#34; outerText&#34);