我对编码非常陌生,我正在尝试使用Selenium Chrome网络驱动程序在Nike.com上制作表单填充程序。然而,弹出一个关于烹饪的弹出窗口,我发现很难将其删除,所以我可以填写表格。 This is what it looks like 我的代码看起来像这样:
<?php
function sidebar_tag_cloud_5416__local_agents() {
// List in order of estate agents page
$selected_agents = [183, 184, 182, 181, 180, 179, 178, 177, 176, 174, 173, 258, 172, 171];
$tags_count = 6;
// Let's select 6 random tag ID's
$selected_agents = array_intersect_key($selected_agents, array_flip(array_rand($selected_agents, $tags_count)));
$selected_agents = array('include' => implode(',', $selected_agents));
// Retrieving tags
$tags = get_tags($selected_agents);
// List output
echo '<ul id="tag-cloud-sidebar">';
if (!empty($tags)) {
foreach($tags as $tag) {
// image id is stored as term meta
$image_id = get_term_meta($tag->term_id, 'image', true);
// image data stored in array, second argument is which image size to retrieve
$image_data = wp_get_attachment_image_src($image_id, 'tag_img');
// image url is the first item in the array (aka 0)
$image = $image_data[0];
if (!empty($image)) {
echo '<li><a href="'. get_tag_link($tag->term_id) . '">';
echo '<img title="' . $tag->name . '" alt="' . $tag->name . '" style="width: 160px;" src="' . esc_url($image) . '"/>';
echo '</a></li>';
}
}
}
echo '</ul>';
}
当我运行此代码时,我收到此错误:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#Initialise a chrome browser and return it
def initialisebrowser():
browser=webdriver.Chrome(r'''C:\Users\ben_s\Downloads\chromedriver''')
return browser
#Pass in the browser and url, and go to the url with the browser
def geturl(browser, url):
browser.get(url)
#Initialise the browser (and store the returned browser)
browser = initialisebrowser()
#Go to a url(nike.com) with the browser
geturl(browser,"https://www.nike.com/gb/en_gb/s/register")
button = browser.find_element_by_class_name("nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button")
button.click()
这个问题的任何想法,指针或解决方案都非常适用
答案 0 :(得分:0)
使用time.sleep
等待弹出窗口加载,之后您可以使用cookie-settings-button-container
类btn
这个可以使用的类。
time.sleep(1)
button = browser.find_element_by_class_name("cookie-settings-button-container")
button.click()
答案 1 :(得分:0)
find_element_by_class_name
将一个类作为参数
browser.find_element_by_class_name('yes-button')
您提供的参数是所有webelement类的组合,由css_selector
browser.find_element_by_css_selector('.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')
请注意,您还需要在第一个类之前添加.
,否则将其视为标记名称。例如,在这种情况下
browser.find_element_by_css_selector('button.nsg-button.nsg-grad--nike-orange.yes-button.cookie-settings-button.js-yes-button')
要确保该按钮在点击之前存在,您可以使用explicit wait
button = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.CLASS_NAME, 'yes-button')))
button.click()