我正在尝试编写代码来访问 Aliexpress 并搜索商品,然后提取详细信息,例如产品名称、价格等;逐页转换为 Excel 文档。我通过之前发布的问题来构建它。多亏了这一点。
不知何故,我能够在前 5 或 6 次测试运行中搜索该项目,但突然间,速卖通要求我登录或注册。
1.) 第一个问题,为什么浏览器不注册就无法访问网站?他们认出了我的用户代理吗?
2.) 其次,然后我编写了一个自动登录的代码。网站包含大量 JavaScript,它是一个响应式网站。当我们点击一些 html 元素时,它们就会出现。自动登录时,我的代码不会检测页面的电子邮件或密码元素。是否有什么东西阻止它被检测到?我该如何解决这个问题?
我在这里放置了我的示例代码:
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
//To input the user's search
Scanner nw1 = new Scanner(System.in);
System.out.println("What do you want to search?");
String a = nw1.nextLine();
//Open the driver
System.setProperty("webdriver.chrome.driver",
"E:\\JetBrains\\webdriver\\chrome\\chromedriver.exe");
WebDriver AE = new ChromeDriver();
//Open the web page and Login in.
AE.get("https://www.aliexpress.com/");
Thread.sleep(2000);
//xpath of account button
AE.findElement(By.xpath("//*[@id="nav-user-account"]/div/div/p[3]/a[2]")).click();
//xpath of Sign in button
AE.findElement(By.xpath("/html/body/div[9]/a")).click();
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//xpath of Email box
AE.findElement(By.xpath("//*[@id=\"fm-login-id\"]")).sendKeys("my-email");
//xpath of password section to type
AE.findElement(By.xpath("//*[@id=\'fm-login-password\']")).sendKeys("my-password");
AE.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// xpath of submit button
AE.findElement(By.xpath("//*[@id="login-form"]/div[5]/button")).click()
抱歉,我第一次来这里。 欢迎任何有帮助的评论。谢谢。
最佳答案
这是因为遵循x路径没有点击帐户按钮而是点击登录按钮
AE.findElement(By.xpath("//*[@id="nav-user-account"]/div/div/p[3]/a[2]")).click();
通过id查找元素。点击账号即可获得id
"nav-user-account"
单击时验证它是否通过以下类展开
"ng-item nav-pinfo-item nav-user-account user-account-unfold"
如果展开包含则框打开,否则框关闭。如果关闭则单击它。 首先尝试这个。
关于java - 从电子商务网站中提取 Web 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026815/