繁体   English   中英

Selenium和Java的麻烦单击按钮

[英]Trouble Clicking Buttons with Selenium and Java

我试图按确实使用硒的主页上的“查找作业”按钮。 我使用了By.id,By.xpath和By.className,但仍无法正常工作。 与我要问的问题有很多类似的问题,但是我已经尝试过了,它们与我已经做的没有什么不同。 如果那很重要,我正在使用Opera浏览器(不想使用其他浏览器),并且版本是3.4.0。

执行后没有显示错误。

再次的问题是,如何在Opera浏览器上使用selenium和java单击按钮?

感谢帮助。

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.opera.OperaOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumLearning 
{
    WebDriver driverC;  
    WebDriver driverO;

public static void main(String[] args) throws InterruptedException
{
    SeleniumLearning SL = new SeleniumLearning();
    //SL.invokeOperaBrowser();
    SL.indeedOpera();
    //SL.yahooOpera();
    //SL.duckduckgoOpera();
}
public void indeedOpera() throws InterruptedException
{
    System.setProperty("webdriver.opera.driver", "C:\\Program Files (x86)\\Selenium Stuff\\operadriver_win64\\operadriver.exe");
    driverO = new OperaDriver();
    //driverO.manage().deleteAllCookies();
    driverO.manage().window().maximize();
    driverO.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driverO.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

    driverO.get("http://www.indeed.com");
    driverO.findElement(By.className("input_text")).sendKeys("HR");

    //Trying different methods
    //These don't work individually either
    Thread.sleep(3000);
    driverO.findElement(By.id("fj")).click();
    Thread.sleep(3000);
    driverO.findElement(By.xpath("//*[@id='fj']")).click();
    Thread.sleep(3000);
    driverO.findElement(By.className("inwrapBorderTop")).click();
}
}

尝试使用JS代码单击以下元素:driver.get(“ http://www.indeed.com ”); driver.findElement(By.className(“ input_text”))。sendKeys(“ HR”);

Thread.sleep(1000);

WebElement buttontoclick=driver.findElement(By.id("fj"));

((JavascriptExecutor) driver).executeScript("arguments[0].click();", buttontoclick);

当我直接查看站点时,我首先手动执行了测试。 我输入“ hr”,然后单击按钮。 那时,可以通过XPath // * [@ id =“ fj”]访问该按钮

然后,我尝试再次返回以查看按钮的定位器是否仅在存在搜索文本的情况下才启用,并且我立即注意到屏幕布局不再相同。 我想如果清除cookie也会,但是我还注意到,在第二次尝试时,它已经在文本框中包含了我以前的文本,但是没有显示按钮的定位符,只显示了XPath // * [@ id = “ jobsearchform”] / div [5] /输入

我不知道第二个定位器是否可以与网页的第一次加载一起使用,但是在任何一种情况下都应该可以使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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