英文:
How to extract the text 73 from the text 1-16 of 73 results from the search result summary within https://www.amazon.com using Selenium and Java
问题
我正在导航到 https://www.amazon.com/
在那里,我正在寻找 'samsung tv 55 inch' 并将其设置在搜索框文本字段中
然后,我试图提取 (63 个结果 [见附图]) 的文本:
我无法找到正确的定位方式和如何找到它,这是我的代码:
package com.bottomline.automation.tests.ui;
import com.bottomline.automation.pageobjects.model.AmazonWebPage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class AmazonTest extends BaseTest {
AmazonWebPage amazonWebPage;
@BeforeClass
public void setup() {
amazonWebPage = new AmazonWebPage(driverWrapper);
}
@Test(priority = 10)
public void navigateToAmazonWebPage(){
amazonWebPage.navigateAndVerify();
}
@Test(priority = 20)
public void searchForHarryPotter(){
amazonWebPage.setSearchTextSearchBox("samsung tv 55 inch");
}
}
我在寻找正确的定位方式以获取结果文本方面遇到了困难
英文:
I am navigating to https://www.amazon.com/
there I am looking for 'samsung tv 55 inch' setting it in the search box text field
then I am trying to extract the text of (63 results [see image attached]):
I can't find the correct locator and how to find it, this is my code:
package com.bottomline.automation.tests.ui;
import com.bottomline.automation.pageobjects.model.AmazonWebPage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class AmazonTest extends BaseTest {
AmazonWebPage amazonWebPage;
@BeforeClass
public void setup() {
amazonWebPage = new AmazonWebPage(driverWrapper);
}
@Test(priority = 10)
public void navigateToAmazonWebPage(){
amazonWebPage.navigateAndVerify();
}
@Test(priority = 20)
public void searchForHarryPotter(){
amazonWebPage.setSearchTextSearchBox("samsung tv 55 inch");
}
}
I am struggling in finding the correct locator in order to get the result text
答案1
得分: 2
代码部分不要翻译,只返回翻译好的内容:
从文本 1-16 of 73 results for "samsung tv 55 inch" 中提取文本 73,您需要使用 WebDriverWait 来等待 visibilityOfElementLocated()
,您可以使用以下任一 定位策略:
-
使用 cssSelector 和
split()
:String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" "); System.out.println(cssParts[2]);
-
使用 xpath 和
split()
:String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" "); System.out.println(xpathParts[2]);
-
控制台输出:
72
完整的代码块如下:
-
代码块:
driver.get("https://www.amazon.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.nav-input#twotabsearchtextbox"))).sendKeys("samsung tv 55 inch"); driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click(); String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" "); System.out.println(cssParts[2]); String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" "); System.out.println(xpathParts[2]);
-
控制台输出:
75 75
英文:
To extract the text 73 from the text 1-16 of 73 results for "samsung tv 55 inch" you have to induce WebDriverWait for the visibilityOfElementLocated()
and you can use either of the following Locator Strategies:
-
Using cssSelector and
split()
:String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" "); System.out.println(cssParts[2]);
-
Using xpath and
split()
:String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" "); System.out.println(xpathParts[2]);
-
Console Output:
72
Complete code block
Here is the complete solution
-
Code Block:
driver.get("https://www.amazon.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.nav-input#twotabsearchtextbox"))).sendKeys("samsung tv 55 inch"); driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click(); String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" "); System.out.println(cssParts[2]); String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" "); System.out.println(xpathParts[2]);
-
Console Output:
75 75
答案2
得分: 1
尝试通过部分文本识别对象。对我来说,这将返回一个唯一的匹配:
//span[contains(text(), 'results for')]
从中你可以获取文本,它应该返回完整的字符串。
英文:
Try identifying the object by partial text. For me, this returns a unique hit:
//span[contains(text(), 'results for')]
From that you can get text and it should return the full string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论