簡體   English   中英

無法使用 selenium/python 單擊特定元素(嘗試了 iframe 以及我知道或在這里找到的所有方法)

[英]Can't click on a specific element using selenium/python (tried iframe and all the methods that I know or found here)

我正在嘗試使用 Selenium 和 Python 訪問網站。

加載網站后,網站上方會出現一個 iframe/彈出窗口,我需要點擊“Alles akzeptieren”,但無論我嘗試什么方法,我都無法定位該元素。

我將我使用的所有方法都注釋掉了,但沒有一個有效。

import re
import time
from time import sleep
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import os,sys, glob
import requests
import win32api
import openpyxl
import xlsxwriter
import io
import csv
from csv import writer
import getpass
from time import sleep
import pandas as pd
import pyperclip as pc
import shutil
import requests
import win32api
from datetime import datetime, date


timestampstart = time.strftime("%Y%m%d-%H-%M-%S")
print("start: " + timestampstart)


user= os.environ.get('USERNAME')
file_name = str(os.path.basename(sys.argv[0]))


now = datetime.now()
path = os.path.abspath(".")
runPath = path
usrName = getpass.getuser()

print("Automation started!")



os.environ['WDM_LOG_LEVEL'] = '0'
options = Options()
options.headless = False######

options.add_argument("--log-level=3")
options.add_argument("--disable-logging")
prefs = {'download.default_directory' : runPath}
options.add_experimental_option('prefs', prefs)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
selenium = webdriver.Chrome(ChromeDriverManager().install(), options=options)
selenium.maximize_window()



  
wait = WebDriverWait(selenium, 60, poll_frequency=1)


selenium.execute_script("window.open('about:blank','firsttab');")
selenium.switch_to.window("firsttab")      
selenium.get('https://www.conrad.de/')
WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
selenium.implicitly_wait(1)
time.sleep(1)

################################################################################################################################
####################### switch to iframe id uc-cross-domain-bridge


#selenium.switch_to.frame(selenium.find_elements_by_tag_name("iframe")[0])
#selenium.switch_to.frame(selenium.find_element_by_id("uc-cross-domain-bridge"))
#selenium.find_element_by_xpath("//button[contains(., 'Alle')]").click()
#yourElement = selenium.find_elements_by_xpath(".//button[@class='sc-gsDKAQ gvhqmO']")    
#selenium.execute_script("arguments[0].click();", yourElement)

selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
but = selenium.find_elements_by_tag_name('div')
for b in but:
    if 'Alle' in b.text:
        try:
            b.click()
        except:
            selenium.execute_script("arguments[0].click();", b)
            time.sleep(1)
            break
selenium.switch_to_default_content()




try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass

不知道為什么我得到 -1 ......但是,發布我最近的嘗試:

import re
import time
from time import sleep
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import os,sys, glob
import requests
import win32api
import openpyxl
import xlsxwriter
import io
import csv
from csv import writer
import getpass
from time import sleep
import pandas as pd
import pyperclip as pc
import shutil
import requests
import win32api
from datetime import datetime, date


timestampstart = time.strftime("%Y%m%d-%H-%M-%S")
print("start: " + timestampstart)


user= os.environ.get('USERNAME')
file_name = str(os.path.basename(sys.argv[0]))


now = datetime.now()
path = os.path.abspath(".")
runPath = path
usrName = getpass.getuser()

print("Automation started!")



os.environ['WDM_LOG_LEVEL'] = '0'
options = Options()
options.headless = False######

options.add_argument("--log-level=3")
options.add_argument("--disable-logging")
prefs = {'download.default_directory' : runPath}
options.add_experimental_option('prefs', prefs)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
selenium = webdriver.Chrome(ChromeDriverManager().install(), options=options)
selenium.maximize_window()



  
wait = WebDriverWait(selenium, 60, poll_frequency=1)


selenium.execute_script("window.open('about:blank','firsttab');")
selenium.switch_to.window("firsttab")      
selenium.get('https://www.conrad.de/')
WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
selenium.implicitly_wait(1)
time.sleep(1)

################################################################################################################################
####################### switch to iframe id uc-cross-domain-bridge


#selenium.switch_to.frame(selenium.find_elements_by_tag_name("iframe")[0])
#selenium.switch_to.frame(selenium.find_element_by_id("uc-cross-domain-bridge"))
#selenium.find_element_by_xpath("//button[contains(., 'Alle')]").click()
#yourElement = selenium.find_elements_by_xpath(".//button[@class='sc-gsDKAQ gvhqmO']")    
#selenium.execute_script("arguments[0].click();", yourElement)
#try:
#    selenium.find_element_by_xpath("//*[contains(text(), 'Alles akzeptieren')]").click()
#except:
#    selenium.find_element_by_xpath("//div[.='Alles akzeptieren']/parent::button").click()
try:
    selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alles akzeptieren' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
    selenium.switch_to.default_content()
except:
    pass



try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass
#####################################
selenium.switch_to.frame(selenium.find_element_by_css_selector("body>iframe"))
but = selenium.find_elements_by_tag_name('div')
for b in but:
    if 'Alles akzeptieren' in b.text:
        try:
            b.click()
        except:
            selenium.execute_script("arguments[0].click();", b)
            time.sleep(1)
            break
selenium.switch_to.default_content()




try:
    WebDriverWait(selenium, 3).until(lambda driver: selenium.execute_script('return document.readyState') == 'complete')
    but = selenium.find_elements_by_tag_name('div')
    for b in but:
        if 'Alle' in b.text:
            try:
                b.click()
            except:
                selenium.execute_script("arguments[0].click();", b)
                time.sleep(1)
                break
except:
    pass
#################################################################################################################################

try:
    butt = selenium.find_elements_by_class_name('sc-gsDKAQ gvhqmO')
    butt.click()
    selenium.execute_script("arguments[0].click();", butt)
    for bb in butt:
        if 'Alles' in bb.text:
            try:
                bb.click()

            except:
                selenium.execute_script("arguments[0].click();", bb)
                time.sleep(1)
                break
except:
    pass
a = selenium.find_element_by_class_name('ccpAEM')
aa = a.find_element_by_id('usercentrics-root')
b = aa.find_element_by_class_name('root responsivegrid')
aaa = b.find_element_by_class_name('sc-bdvvtL ljqtvs')#
aaaa = aaa.find_element_by_id('focus-lock-id')
aaaaa = aaaa.find_element_by_id('focus-lock-id')
aaaaaa = aaaaa.find_element_by_class_name('sc-pVTFL')# gsAnRF
aaaaaaa = aaaaaa.find_element_by_class_name('sc-jrQzAO')# CTXfe
aaaaaaaa = aaaaaaa.find_element_by_class_name('sc-bYoBSM')# egarKh
aaaaaaaaa = aaaaaaaa.find_element_by_class_name('sc-kLwhqv')# fslbnd
aaaaaaaaaa = aaaaaaaaa.find_element_by_class_name('sc-dlVxhl')# bEDIID
aaaaaaaaaaa = aaaaaaaaaa.find_element_by_class_name('sc-kfPuZi')# jQTRpX
aaaaaaaaaaaa = aaaaaaaaaaa.find_element_by_class_name('sc-gsDKAQ')# gvhqmO
print(aaaaaaaaaaaa.text)

仍然無法定位元素它使用影子根

嘗試使用https://pypi.org/project/pyshadow/來 select 你的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM