硒/更多元素点击总是不可能的?
Selenium / More-Element clicking not possible allways?
我尝试点击这些页面上的更多元素 - 使用以下代码
对于以下 link 这适用于根据 xpath 查找元素:
import os, sys, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from sys import platform
WAIT = 1
link = "https://apps.apple.com/us/app/daily-budget-original/id651896614?uo=4"
path = os.path.abspath (os.path.dirname (sys.argv[0]))
if platform == "win32": cd = '/chromedriver.exe'
elif platform == "linux": cd = '/chromedriver'
elif platform == "darwin": cd = '/chromedriver'
options = Options()
options.add_argument("--window-size=1920x1080")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
# options.add_argument('--headless')
driver = webdriver.Chrome (path + cd, options=options)
driver.get (link) # Read link
time.sleep (WAIT) # Wait till the full site is loaded
driver.find_element_by_xpath('//*[@id="ember-app"]/div/main/div[2]/section[7]/div[1]/dl/div[9]/dd/ol/div/button').click()
但是当我用另一个 link / 应用程序尝试相同的代码时
https://apps.apple.com/us/app/contacts-backup-pro-restore/id1120943403?uo=4
更多元素不再打开
我看到 xpath 略有不同,所以我尝试了
driver.find_element_by_xpath('//*[@id="ember-app"]/div/main/div[2]/section[9]/div[1]/dl/div[9]/dd/ol/div/button').click()
但这不起作用
然后我尝试用 class-选择器代替
driver.findElement(By.cssSelector("button[class='we-truncate__button we-truncate__button--top-offset link']")).click()
但这对 link 两个现在都不起作用...
有没有办法同时获得两个 links 运行 - 在最好的情况下使用 css-选择器?
是的,你可以使用下面的 xpath
:
//dt[text()='In-App Purchases']/following-sibling::dd/descendant::button
这应该在两个页面上点击更多。
Xpath 基本上是在寻找 In-App Purchases
,然后是其中的以下按钮,因此您可以在任何应用商店网页上使用
你可以这样点击它:
wait = WebDriverWait(driver, 10)
in_app_more_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//dt[text()='In-App Purchases']/following-sibling::dd/descendant::button")))
in_app_more_button.click()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我尝试点击这些页面上的更多元素 - 使用以下代码
对于以下 link 这适用于根据 xpath 查找元素:
import os, sys, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from sys import platform
WAIT = 1
link = "https://apps.apple.com/us/app/daily-budget-original/id651896614?uo=4"
path = os.path.abspath (os.path.dirname (sys.argv[0]))
if platform == "win32": cd = '/chromedriver.exe'
elif platform == "linux": cd = '/chromedriver'
elif platform == "darwin": cd = '/chromedriver'
options = Options()
options.add_argument("--window-size=1920x1080")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
# options.add_argument('--headless')
driver = webdriver.Chrome (path + cd, options=options)
driver.get (link) # Read link
time.sleep (WAIT) # Wait till the full site is loaded
driver.find_element_by_xpath('//*[@id="ember-app"]/div/main/div[2]/section[7]/div[1]/dl/div[9]/dd/ol/div/button').click()
但是当我用另一个 link / 应用程序尝试相同的代码时 https://apps.apple.com/us/app/contacts-backup-pro-restore/id1120943403?uo=4 更多元素不再打开
我看到 xpath 略有不同,所以我尝试了
driver.find_element_by_xpath('//*[@id="ember-app"]/div/main/div[2]/section[9]/div[1]/dl/div[9]/dd/ol/div/button').click()
但这不起作用
然后我尝试用 class-选择器代替
driver.findElement(By.cssSelector("button[class='we-truncate__button we-truncate__button--top-offset link']")).click()
但这对 link 两个现在都不起作用...
有没有办法同时获得两个 links 运行 - 在最好的情况下使用 css-选择器?
是的,你可以使用下面的 xpath
:
//dt[text()='In-App Purchases']/following-sibling::dd/descendant::button
这应该在两个页面上点击更多。
Xpath 基本上是在寻找 In-App Purchases
,然后是其中的以下按钮,因此您可以在任何应用商店网页上使用
你可以这样点击它:
wait = WebDriverWait(driver, 10)
in_app_more_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//dt[text()='In-App Purchases']/following-sibling::dd/descendant::button")))
in_app_more_button.click()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC