将 Selenium 用于您不拥有的网站时,切勿依赖 ID 或类,因为它们经常更改,尤其是对于 google 的网站。
搜索它的最佳方法是找到显示您知道在按钮上写的文本的元素(在本例中为“请求加入”),然后让所有父级循环并检查其中一些是否是按钮。
像这样:
WebElement buttonTextElement = browser.find_elements_by_xpath("//*[contains(text(), 'Ask to join')]")
然后循环启动此javascript代码并仅在父角色属性等于“按钮”或标记为“按钮”时停止它
WebElement parent = buttonTextElement;
WebElement parent = browser.execute_script("return arguments[0].parentNode;", parent)
然后单击()。
我已经用 Java 为 ua 编写了完整的工作代码。我使用的是 chromedriver 版本 85。
我不应该粘贴整个代码,但我会为你做的:)
我看到“下一个”按钮是第一个父级,所以你不需要递归。PS:由于我访问了意大利网页,请确保字符串“Next”和“Ask to Join”是正确的char for char。如果需要,请更改它们。
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String email = "Your Google Email";
String pass = "Your Google Password";
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// You need this to stop the page from askin u for mic
options.addArguments("--use-fake-ui-for-media-stream");
WebDriver driver = new ChromeDriver(options);
//Login to Google
driver.get("https://accounts.google.com/login");
ArrayList<WebElement> emailinput = new ArrayList<WebElement>();
ArrayList<WebElement> spans = new ArrayList<WebElement>();
emailinput = (ArrayList<WebElement>) driver.findElements(By.tagName("input"));
//Get all spans in page
spans = (ArrayList<WebElement>) driver.findElements(By.tagName("span"));
for(int i = 0; i < emailinput.size(); i++) {
if(emailinput.get(i).getAttribute("type").equals("email")) { emailinput.get(i).sendKeys(email); break; }
}
for(int i = 0; i < spans.size(); i++) {
if(spans.get(i).getText().equals("Next")) {
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].parentNode;", spans.get(i)); parent.click(); break; }
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<WebElement> passinput = (ArrayList<WebElement>) driver.findElements(By.tagName("input"));
for(int i = 0; i < passinput.size(); i++) {
if(passinput.get(i).getAttribute("type").equals("password")) { passinput.get(i).sendKeys(pass); break; }
}
spans = (ArrayList<WebElement>) driver.findElements(By.tagName("span"));
for(int i = 0; i < spans.size(); i++) {
if(spans.get(i).getText().equals("Next")) {
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].parentNode;", spans.get(i)); parent.click(); break; }
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Create a Meet room and put here its URL
driver.navigate().to("https://meet.google.com/dxz-dbwt-tpj");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spans = (ArrayList<WebElement>) driver.findElements(By.tagName("span"));
for(int i = 0; i < spans.size(); i++) {
if(spans.get(i).getText().equals("Ask to Join")) {
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].parentNode;", spans.get(i)); parent.click(); break; }
}
}
}