3 回答

TA贡献1828条经验 获得超6个赞
出于某种原因,您需要双击该图标:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Firefox()
# Log into Google.
url = "https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2F&flowName=GlifWebSignIn&flowEntry=ServiceLogin"
driver.get(url)
time.sleep(1)
username = driver.find_element_by_id('identifierId')
username.send_keys("REDACTED")
time.sleep(1)
driver.find_element_by_id('identifierNext').click()
time.sleep(1)
password = driver.find_element_by_name('password')
password.send_keys("REDACTED")
time.sleep(1)
driver.find_element_by_id('passwordNext').click()
time.sleep(1)
url="https://contacts.google.com/"
driver.get(url)
time.sleep(1)
# Select the first contact and click on it to open the desired menu.
contact = driver.find_element_by_css_selector("div[role='checkbox']")
contact.click()
time.sleep(1)
# Double click the selected action icon to open menu.
svgicon = driver.find_element_by_css_selector("div[data-tooltip='Selection actions']")
ActionChains(driver).move_to_element(svgicon).double_click().perform()
time.sleep(1)
# Click the "All" button.
selectall = driver.find_element_by_xpath("//*/div[text()='All']")
selectall.click()

TA贡献1805条经验 获得超9个赞
在您的第一个代码示例中,
svgicon = driver.find_element_by_css_selector('div.PFdmz .uzHa0d
.RANAid[role="button"]')
选择器匹配四个元素,您要查找的复选框是第四个。如果您还没有,请尝试
svgicon = driver.find_elements_by_css_selector('div.PFdmz .uzHa0d
.RANAid[role="button"]')[3]
而不是那条线。
我立即确定 JS 执行器,但如果上面的代码不起作用,我可以看看。

TA贡献1806条经验 获得超5个赞
我不知道为什么,但是Dan-dev 的代码会执行您需要的结果,我已经尝试过了,对我来说效果很好。在这种情况下,当我遇到像您这样的类似问题时,我将提供另一种替代方法,以替代我之前所做的工作。这是我的代码:
from selenium.webdriver.support.ui import Select
#From Dan-dev's code
svgicon = driver.find_element_by_css_selector("div[data-tooltip='Selection
actions']")
ActionChains(driver).move_to_element(svgicon).double_click().perform()
#Alternative/Optional Solution
driver.execute_script("return arguments[0].removeAttribute('style');", svgicon)
selectall = Select(svgicon)
for option in selectall.options:
selectall.select_by_visible_text('All')
添加回答
举报