简体   繁体   中英

How Do I Click On This “Sign In” Link Using Selenium & Java?

quicknav">
<div class="status-loggedin" style="display:none;" data-component="quicknav-status-loggedin">
<div class="status-loggedout" data-component="quicknav-status-loggedout">
<a class="login-link" data-component="quicknav-login-link" rel="nofollow" title="Sign In" href="#">Sign In</a>
<a class="create-account-link" data-component="quicknav-create-account-link" rel="nofollow" title="Create Your Account" href="#">Create Your Account</a>
</div>
</div>

I tried using below but not working. Please help:

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[contains(@id, 'userInfoQuickNav'/ title = 'Sign In'"));

Your XPath isn't valid and it doesn't match HTML you provided. What you do you mean "not working"? Nothing happens or exceptions? If there are exceptions, post exact stacktrace please.

Here I can take a guess.

WebElement linkSignIn = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//div[contains(@id, 'userInfoQuickNav')]//a[@title='Sign In']")));
linkSignIn.click();

You can also use CSS selector, which is better.

By.cssSelector("[id*='userInfoQuickNav'] a[title='Sign In']")

您也可以尝试linkText

new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Sign In"))).click();

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