网站网址:https://staging.corporatehousingbyowner.com/
嗨,我无法通过使用Java的Selenium找到并单击UL > li中的以下按钮。使用Chrome驱动程序执行自动执行。
<ul>
<li><a class="list-your-prop" href="/dashboard/properties/add/">List Your Property</a></li>
<ul/>已经尝试了以下解决方案。
1:
WebElement element = driver.findElement(By.linkText("List Your Property"));
element.click();2:
WebElement element = driver.findElement(By.className("menu-text"));
String elementText = element.getText();3:
WebElement element = driver.findElement(By.xpath("//li[contains(@class,'list-your-prop')]/ul/li/a"));
element.click();4:driver.findElement(By.cssSelector("body > header:nth-child(1) > nav:nth-child(4) > div:nth-child(1) > div:nth-child(2) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)")).click();
WebPage要闻1:https://i.stack.imgur.com/FH2LR.png
请帮忙,提前谢谢。
发布于 2021-09-13 12:04:42
有两个带有List Your Property.的 web元素
下面的xpath在HTMLDOM中有唯一的条目
//div[@id='myNavbar']/descendant::a[text()='List Your Property']代码试用1 :
WebElement element = driver.findElement(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']"));
element.click();代码试用版2:具有显式等待的:
WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']")));
element.click();更新1 :
driver.manage().window().maximize();
//WebDriverWait wait = new WebDriverWait(driver, 30);
//driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
driver.get("https://staging.corporatehousingbyowner.com/");
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']")));
element.click();发布于 2021-09-13 12:29:54
您可以使用
driver.findElement(By.cssSelector("nav.navbar a.list-your-prop")).click();
使用等待程序也更好,因为加载元素可能需要时间。
发布于 2021-09-25 18:41:04
--您只需要从父->子节点遍历来识别惟一的xpath,因为它仅返回带有类属性的2个元素。以下是工作代码:
driver.get("https://staging.corporatehousingbyowner.com/")
driver.implicitly_wait(10)
driver.find_element_by_xpath("//ul[@class='nav navbar-nav navbar-right']//a[text()='List Your Property']").click()https://stackoverflow.com/questions/69162236
复制相似问题