繁体   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