【问题标题】:Selenium WebDriver -- XPathSelenium WebDriver——XPath
【发布时间】:2017-11-06 18:19:56
【问题描述】:

试图让脚本选择顶部的过滤器按钮,但似乎无法弄清楚如何输入 XPath。我相信这与它在单独的 iframe 中有关。

package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class JavaClass {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://mlvtwrew73.consilio.com/Relativity/");
		driver.manage().window().maximize();
		//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
		
		WebElement objWE;
		Thread.sleep(9000);
		// objWE = driver.findElement(By.linkText("User Status"));
		// objWE.click();

		driver.switchTo().defaultContent();		
		driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
			
		// objWE = driver.findElement(By.id("1"));
		
		// driver.close(); will be used to close the site once all testing completes

	}

}

【问题讨论】:

  • 如果在单独的frame中,需要switchTo()那个frame,然后再次执行xpath。
  • 所以我将它添加到脚本中,但仍然出现同样的错误:driver.switchTo().frame("ListTemplateFrame"); driver.switchTo().defaultContent(); driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")/img[1]")).click();

标签: java selenium iframe xpath


【解决方案1】:

使用 ID 定位器——在这里更合适(并且比 XPath 更快):

WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ct‌l00_ctl00_itemList_F‌​ilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();

【讨论】:

  • 这就是我最初使用的,但似乎没有用,所以我想我会使用 xpath。我尝试了上述方法,但不幸的是没有用,您还需要我提供的其他帮助吗?谢谢
  • 无法定位元素:{"method":"id","selector":"ctl00_ctl00_itemList_FilterSwitch"}
  • @Jesus,在执行点击操作之前使用WebDriverWait wait= new WebDriverWait(driver, 20); return wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_ctl00_itemList_FilterSwitch")));
  • 抱歉,您想我在哪里输入这个?
  • 好的,所以我想通了。这是我所做的与您要求我做的类似........ driver.switchTo().defaultContent(); WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 20); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//*[@id=\"ListTemplateFrame\"]"))); driver.findElement(By.xpath("//*[@id=\"ctl00_ctl00_itemList_FilterSwitch\"]")).click();
【解决方案2】:

看起来您传递的 XPath 不正确。试试这个:

driver.findElement(By.xpath("//a[@id='ctl00_ctl00_itemList_FilterSwitch']")).click();

【讨论】:

  • 是的,我之前尝试过这个 sn-p,但没有成功,现在又试了一次,但控制台说 Unable to locate element: {"method":"xpath","selector":"// div[@class='actionCellContainer']//a/img[@class='itemListActionImage']"}
  • 您没有使用我推荐的 XPath。它显示您尝试使用的 XPath 是:"//div[@class='actionCellContai‌​ner']//a/img[@class=‌​'itemListActionImage‌​']。我告诉过您使用“//一个[@id='ctl00_ctl00_itemList_FilterSwitch']"。
  • 哎呀可能回复了错误的线程。我的坏..这是你给我的正确错误:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//a[@id='ctl00_ctl00_itemList_FilterSwitch']”}
  • 解决方案:driver.switchTo().defaultContent(); WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 20); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//*[@id=\"ListTemplateFrame\"]"))); driver.findElement(By.xpath("//*[@id=\"ctl00_ctl00_itemList_FilterSwitch\"]")).click();
【解决方案3】:

如果有那么你使用 xpath 的原因。

像这样使用id

  driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();

【讨论】:

    【解决方案4】:

    我需要更清楚地了解您的问题。 据我了解,我认为您需要过滤器图像的 XPath。如果我是对的,试试这个:

    //div[@class='actionCellContainer']//a/img[@class='itemListActionImage']
    

    【讨论】:

    • 澄清:所以脚本正在打开页面,但是当它去点击过滤器图标时。它什么也不做。我试图嵌入按钮的 xpath,但这似乎不起作用。我在谷歌上进行了一些研究,发现其他几个人也遇到了同样的问题,并且不得不对位于单独 iframe 中的元素做一些事情。但未能解释他们是如何解决的。我尝试了上述方法,但没有奏效。还有什么我可以为您提供的可能对您有帮助的吗?
    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2018-09-04
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多