简体   繁体   中英

Selenium: cssSelector and path both cannot find an element

So I've tried to select this element by both cssSelector and Xpath but no luck. It returns the error no such element. But this is the closest i've gotten to grabbing the element with the below code.

WebElement x = driver.findElement(By.cssSelector("meta[property='og\\:title']"));

or

WebElement x = driver.findElement(By.xpath("//meta[@property='og\\:title']"));

However, it still provides the error of invalid selector or no such element.

The Xpath I've tried is below. The element is below. If you want to the website it is also below.

/html/head/meta[14]

HTML:

<meta property="og:title" content=" Blydenburgh County Park Stump Pond Loop" class="xlate-none">

URL: www.alltrails.com/explore/trail/us/new-york/blydenburgh-county-park-stump-pond-loop

You have unnecessary symbols \\ after og . So your code can't find the path. Try like this

WebElement x = driver.findElement(By.xpath("//meta[@property='og:title']"));

To locate the element with text as Blydenburgh County Park Stump Pond Loop within the url https://www.alltrails.com/explore/trail/us/new-york/blydenburgh-county-park-stump-pond-loop?u=m , as the the desired element is within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt .

  • Induce WebDriverWait for the desired elementToBeClickable .

  • You can use either of the following Locator Strategies :

  • Using cssSelector :

     driver.get("https://www.alltrails.com/explore/trail/us/new-york/blydenburgh-county-park-stump-pond-loop?u=m"); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe#trail-details-frame"))); WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1[title=' Blydenburgh County Park Stump Pond Loop']")));
  • Using xpath :

     driver.get("https://www.alltrails.com/explore/trail/us/new-york/blydenburgh-county-park-stump-pond-loop?u=m"); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='trail-details-frame']"))); WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[@title=' Blydenburgh County Park Stump Pond Loop']")));

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