Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium,JAVA-如何找到这样的元素?_Java_Testing_Selenium_Webdriver - Fatal编程技术网

Selenium,JAVA-如何找到这样的元素?

Selenium,JAVA-如何找到这样的元素?,java,testing,selenium,webdriver,Java,Testing,Selenium,Webdriver,您知道如何查找并单击如下元素: <div class="offer ctrlOpenOfferInfo"> <a class="linkOffer" href="offer_view.html?id=1007"/> <p class="photo"> <p class="name">New offer</p> <p class="price">123</p> <div class="rate ctrlVie

您知道如何查找并单击如下元素:

<div class="offer ctrlOpenOfferInfo">
<a class="linkOffer" href="offer_view.html?id=1007"/>
<p class="photo">
<p class="name">New offer</p>
<p class="price">123</p>
<div class="rate ctrlViewRateOffer" data-value="0.0000">
<p class="date"/>
<div class="hide info">
<div class="bgInfo"/>
或:

或:

或:


我已经用文本、类、xpath…

尝试了这些方法,假设您要单击包含新报价的p标记: 我认为这个元素不在iframe中。如果是这样,则必须使用driver.switchTo.framename或xpath或css作为帧定位器;首先,在开始查找元素之前。 现在,您如何找到该元素。考虑以下选项:

使用基于xpath文本的搜索

您可以使用By.className'name'来标识该元素,即使该名称对我来说不是唯一的

使用cssSelector

.姓名

使用cssSelector的第n个子函数


.offer.ctrlopenoferinfo>a>p:nth-child2

我已经尝试过了,但这不是一个好主意,因为这是我刚刚创建的一个元素,我想打开它的页面,以后每次我运行测试时,页面上都会有不同数量的类似元素,我每次都要数一下,然后将正确的数字输入xpath。它的xpath是:./*[@id='formDevelopmentElementAdd']/div[14],目前它的编号是14。我尝试在xpath中使用一个变量,但没有成功。我试图找到它的href,但也不起作用。目前没有新想法,需要帮助…然后我建议对每个元素使用id。在这种情况下,您必须实现动态id——这意味着每次添加项/元素时,您也应该生成它的id。然后你可以使用getElementById方法。你所说的元素具体是什么意思?@Saifur我不太确定用英语怎么称呼它,所以我们可以这样说:我有一家公司,我的整个报价会议、文件。。。可以在我正在测试的页面上查看、购买等。每个报价都在一个框架内。我需要找到我刚刚创建的代码,以及我在问题中添加的代码。报价名称是“新报价”。@VivekSingh很多,请参见编辑后的问题。是否正确?driver.switchTo.framedriver.findElementBy.cssSelectora.linkOffer;driver.findElementBy.xpath//p[@class='name'][.='newoffer']]。单击;语法是。我不知道这个页面的html。因此,不确定iframe的选择器是否正确。我在HTML中看不到框架。我用frame这个词来描述元素,并不是说HTML中有frame,我不知道这个词的HTML含义。这是一个类,我的元素在:是否有机会使用其他内容单击该元素?请解释其他内容。你是说JavaScript?
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[text()='New offer']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='formDevelopmentElementAdd'][text()='New offer']")));
driver.findElement(By.xpath(".//*[@id='formDevelopmentElementAdd']/div/p[2][text()='New offer']")).click();
driver.findElement(By.xpath("//p[@class='name'][text()='New offer']")).click();
String address = driver.findElement(By.xpath("//*[contains(text(), 'New offer') and @class='name']")).getAttribute("href");
driver.get(baseUrl + address);
driver.findElement(By.xpath("//a[Text()='New offer']")).click();
//p[.='New offer']
//to be more precise you can do
//p[@classs='name'][.='New offer']