Selenium - webdriver可以找到元素,但不执行点击操作

时间:2016-03-01 10:39:16

标签: java selenium-webdriver

我正在尝试使用Selenium webdriver和Java自动化Web应用程序的功能测试。 在AUT中,有一个'提交'按钮由以下html代码

定义
<button id="submitbtn" class="btn btn-primary" type="submit">Submit</button>

我使用以下命令单击按钮。

driver.findElement(By.id("submitbtn")).click();

当我运行代码时,webdriver可以找到按钮,但是没有执行click操作(我可以理解webdriver可以找到按钮,因为没有抛出异常,我可以在代码上看到按钮上的选项运行)。我尝试了不同的等待

new WebDriverWait(driver,60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn"));

但没有得到任何积极的结果。如果我使用,

Thread.sleep(3000);

它工作正常(但我想避免这个代码)。我尝试了所有其他类型的等待和动作类,

Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.id("submitbtn"));
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("submitbtn")));
action.click().perform();

但没有运气。有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:1)

JavascriptExecutor怎么样?

WebElement element = driver.findElement(By.id("submitbtn"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

答案 1 :(得分:0)

<surface:SurfaceScrollViewer x:Name="listDocumentsVisibility" Visibility="Visible" Grid.Column="1" Grid.Row="0" Margin="15,36,15,35" Background="GhostWhite" VerticalScrollBarVisibility="Hidden" PanningMode="Both"> <ItemsControl ItemsSource="{Binding Path=AttachmentsFileList}"> <ItemsControl.ItemTemplate> <DataTemplate> <surface:SurfaceButton Tag="{Binding ATID}" Click="Meeting_Click"> <Button.Template> <ControlTemplate TargetType="{x:Type Button}"> <StackPanel> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"></ColumnDefinition> <ColumnDefinition Width="1*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="60"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Margin="10,5,0,0" Grid.Column="0"> <Image Width="26" Margin="5,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Source="/Resources/Images/icon-document-browser.png"></Image> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-30,0,0" FontSize="12" Text="{Binding Name}"></TextBlock> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-15,0,0" FontSize="9" Foreground="#6C6C6C" Text="{Binding LastOpenDate}"></TextBlock> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-3,0,0" FontSize="9" Foreground="#6C6C6C" Text="istituto"></TextBlock> </StackPanel> <StackPanel Grid.Row="0" Margin="10,5,0,0" Grid.Column="1"> <Image Width="26" Margin="5,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Source="/Resources/Images/icon-document-browser.png"></Image> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-30,0,0" FontSize="12" Text="{Binding Name}"></TextBlock> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-15,0,0" FontSize="9" Foreground="#6C6C6C" Text="{Binding LastOpenDate}"></TextBlock> <TextBlock FontFamily="{StaticResource Lato Light}" HorizontalAlignment="Left" Margin="38,-3,0,0" FontSize="9" Foreground="#6C6C6C" Text="istituto"></TextBlock> </StackPanel> </Grid> </StackPanel> </ControlTemplate> </Button.Template> </surface:SurfaceButton> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </surface:SurfaceScrollViewer> 是一个选项submit()。更多信息here