[英]Not able to click on Login button using selenium webdriver and Java
Hi I am trying to automate https://www.nextgenerationautomation.com and unable to click on login / SignUp button using Selenium 4嗨,我正在尝试自动化https://www.nextgenerationautomation.com并且无法使用 Selenium 4 来点击登录/注册按钮
Steps:脚步:
Issue: when I am using Thread.Sleep
, code is working fine but when I am using explicit wait & implicit wait it's not working.问题:当我使用Thread.Sleep
时,代码工作正常但是当我使用显式等待和隐式等待时它不起作用。
I have added Implicit in my base class at the time of driver initialization.我在驱动程序初始化时在我的基础 class 中添加了隐式。
Here is the code that I have tried.这是我试过的代码。
public class nextGenAutomationLoginPage extends Base {
@FindBy(xpath = "(//button[@class='_1YW_5'])[1]")
WebElement login;
public nextGenAutomationLoginPage() {
super();
PageFactory.initElements(driver, this);
// TODO Auto-generated constructor stub
}
public void clickOnLogin() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
System.out.println(driver.getTitle());
// Thread.sleep(2000);
wait.until(ExpectedConditions.elementToBeClickable(login));
//wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(login, By.xpath("//div[@class='_10b1I']") ));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
js.executeScript("arguments[0].scrollIntoView();", login);
login.click();
//driver.findElement(By.xpath("(//button[@class='_1YW_5'])[1]")).click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@id='comp-kaoxkn4a1']")));
String sign = driver.findElement(By.xpath("//div[@id='comp-kaoxkn4a1']/h1/span")).getText();
System.out.println(sign);
}
Note: I tried to add Scroll as well to add some wait before clicking.注意:我也尝试添加 Scroll 以在单击之前添加一些等待时间。
DOM: DOM: Please let me know if i am missing anything here.如果我在这里遗漏任何东西,请告诉我。
Hi I got the point whats happening here:嗨,我知道这里发生了什么:
Summery:夏日:
//span[text()='Log In / SignUp']
Xpath 我用过的是//span[text()='Log In / SignUp']
Thanks KS感谢 KS
To click()
on the element with text as Log In / SignUp
you can use either of the following Locator Strategies :要在带有文本的元素上click()
Log In / SignUp
您可以使用以下任一定位器策略:
xpath
: xpath
:
driver.findElement(By.xpath("//span[text()='Log In / SignUp']")).click();
However, as the element is a dynamic element, so to click()
on the element you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies :但是,由于元素是动态元素,因此要在元素上click()
,您需要为elementToBeClickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
xpath
: xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Log In / SignUp']"))).click();
You can find a detailed discussion on NoSuchElementException in:您可以在以下位置找到有关NoSuchElementException的详细讨论:
Actually your code is clicking the login button before the page is loaded properly.实际上,您的代码是在页面正确加载之前单击登录按钮。 please add visibility condition.请添加可见性条件。
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.