python-3.x - 如何使用 Selenium 和 Python 在 https://meet.google.com 中单击“请求加入”按钮?
问题描述
我正在尝试单击 google meet 链接中的 Ask to join 按钮(使用我现有的 Google Chrome 个人资料)。这是代码:
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\\Users\\Pranil.DESKTOP-TLQKP4G.000\\AppData\\Local\\Google\\Chrome\\User Data")
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
delay = 15
browser.get('https://meet.google.com/tws-kcie-aox')
ignored_exceptions=(NoSuchElementException,StaleElementReferenceException,)
time.sleep(5)
join_butt = WebDriverWait(browser, delay ,ignored_exceptions=ignored_exceptions).until(EC.presence_of_element_located((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[5]/div[3]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div')))
join_butt.click()
print(join_butt.text)#this prints Ask to join
但是加入按钮没有被点击。按钮上最奇怪的部分“要求加入”文本确实打印在最后一行。这意味着 selenium 已到达正确的按钮。但是为什么它不点击按钮?
编辑:根据@Alin Stelian 的回答,我将代码更新如下:
browser.get('https://meet.google.com/hst-cfck-kee')
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'd')
join_butt = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Ask to join')]")))
join_butt.click()
print(join_butt)
print(join_butt.text)
这确实有效,因为两个打印语句都有效......但是按钮没有被点击。这里出了什么问题?
解决方案
将 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; }
}
}
}
推荐阅读
- algorithm - Return an index of the most common element in a list of integers with equal probability using O(1) space
- elasticsearch - Automatically remove older zipkin entries in elasticsearch
- kubernetes - GKE 1 负载均衡器,在不同分配的端口上有多个应用程序
- ruby-on-rails - 从 YAML 中提取数据是否比数据库或常量更快?
- python - 为什么 python 正则表达式不匹配特殊字符?
- node.js - 在 DynamoDB 中查询请求获取密钥
- java - 条件,因此一次只能选择一个 ImageView,而不是同时选择两个(如不喜欢 ImageViews)
- html - CSS:我的产品页面侧面的白条
- reactjs - 如何修复此 axios 调用
- ios - 不同屏幕尺寸的标签系列自动布局?