如何在中单击"请求加入"按钮https://meet.google.com使用Selenium和Py



我正试图点击谷歌会议链接中的"请求加入"按钮(使用我现有的谷歌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

但是"加入"按钮没有被点击。按钮上最奇怪的部分"请求加入"文本确实打印在了最后一行。这意味着硒元素已到达正确的按钮。但它为什么不点击按钮?

编辑:根据@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)

这与打印语句一样有效。。。但是按钮没有被点击。这里出了什么问题?

对于进一步的自动化项目-避免在以编程方式生成值时按id查找元素-这对您没有帮助。此外,长xpath对项目性能不利。

定位器的性能水平是->ID,CSS,XPATH。

join_butt=WebDriverWait(浏览器,延迟,ignored_exceptions=ignored_xceptions(.直到(EC.presence_of_element_located((By.XPATH,'//span[包含(text((,'请求加入'(]'((

稍后编辑下次不要忽略异常-这将帮助您查看错误语法,我测试了以下代码。

join_butt = WebDriverWait(browser, delay).until(
EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Ask to join')]")))
driver.execute_script("arguments[0].click();", join_butt)

如果chrome浏览器不允许您登录-这里有一个技巧

  1. 运行您的代码
  2. 在该浏览器中,转到StackOverflow
  3. 使用您的帐户登录
  4. 退出浏览器
  5. 再次运行您的代码-现在您将自动登录到您的谷歌帐户

当您不拥有的网站使用Selenium时,NEVER依赖ID或类,因为它们经常更改,尤其是对于谷歌的网站。

搜索它的最佳方法是找到显示你知道写在按钮上的文本的元素(在这种情况下是Ask to join(,然后让所有的父母都在一个循环中,检查其中是否有按钮。

像这样:

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为您编写了一个完全可用的代码。我用的是chromedriver版本85。

我不应该粘贴整个代码,但我会为你做的:(

我看到了";下一个";Button是第一个父级,所以不需要递归。PS:由于我访问了意大利网页,请确保字符串"下一个";以及";请求加入";是正确的字符对字符。如果需要,请更改它们

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; }

}

}

}


You can use the quickest path 
//span[contains(text(),'Ask to join')]
or from your code correct xpath
//*[@id="yDmH0d"]/c-wiz/div/div/div[4]/div[3]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/span/span
your xpath in the Code
//*[@id="yDmH0d"]/c-wiz/div/div/div[5]/div[3]/div/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div

要打印文本请求加入,您需要诱导WebDriverWait等待visibility_of_element_located(),您可以使用以下定位器策略:

  • 使用XPATHget_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Ask to join']"))).get_attribute("innerHTML"))
    
  • 使用XPATH文本属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Ask to join']"))).text)
    

您可以在"如何使用Selenium-Python 检索WebElement的文本"中找到相关讨论


要点击带有文本的元素请求加入,您需要诱导WebDriverWait等待element_to_be_clickable(),您可以使用以下定位器策略:

  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "//span[text()='Ask to join']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Outro

有用文档链接:

  • get_attribute()方法Gets the given attribute or property of the element.
  • text属性返回The text of the element.
  • 使用Selenium的文本和innerHTML之间的差异

最新更新