[英]Using python and selenium to click a button on a web page
enter image description here在此处输入图像描述
So I'm trying to connect to https://coinmarketcap.com/currencies/bitcoin/ and press the good button which is 3/4;s of the way down.所以我正在尝试连接到https://coinmarketcap.com/currencies/bitcoin/并按下向下 3/4 处的好按钮。
Using Selenium I've tried to use the get_element(By.xpath) and other attributes, when looking at inspect I dont see what I can use as the bottons look the exact same in config.使用 Selenium 我尝试使用 get_element(By.xpath) 和其他属性,在查看检查时我看不到我可以使用什么,因为按钮在配置中看起来完全相同。
should I be using JavaScript to execute the click?我应该使用 JavaScript 来执行点击吗? how would you go about selecting them with them seeming to share the same attributes.
您 go 如何选择它们,因为它们似乎具有相同的属性。 what would be the code to click good and what would it be to click bad, I assume its [0] or [1] is some form of Js?
单击好的代码是什么,单击坏的代码是什么,我假设它的 [0] 或 [1] 是某种形式的 Js?
used selenium attributes, I used By.XPATH, '//*[@id="__next"]/div/div[1]/div[2]/div/div[3]/div/div[1]/div[2]/div[6]/div/div[2]/div/button[1]'使用了selenium属性,我使用了By.XPATH, '//*[@id="__next"]/div/div[1]/div[2]/div/div[3]/div/div[1]/div [2]/div[6]/div/div[2]/div/按钮[1]'
as that seemed as it should work, but I receive.因为这似乎应该有效,但我收到了。 return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
返回 self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
Thanks for any advice in advance.感谢您提前提出任何建议。
Try this:尝试这个:
button_class = BeautifulSoup(driver.page_source, 'html.parser').select('div', class_='button-group')[0].button['class']
button_class = ' '.join(button_class)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//button[@class='{button_class}']"))).click()
As you have the element ID, it is best to search the element by ID:由于您拥有元素 ID,因此最好按 ID 搜索元素:
goodButton = browser.find_element(By.ID,"__next")
This will make sure you always select this element even if it gets displaced from existing location.这将确保您始终 select 这个元素,即使它从现有位置移位也是如此。 For example, an advertisement banner is inserted above it.
例如,在其上方插入广告条幅。
As a rule of thumb remember that a webpage is expected to have unique ID for any element.根据经验,请记住网页的任何元素都应具有唯一 ID。
I checked the page source and couldn't find ID attribute for the "Good"-button.我检查了页面源代码,但找不到“好”按钮的 ID 属性。
I suggest to search:我建议搜索:
Here's the snippet:这是片段:
goodButton = browser.find_element(By.XPATH, '//button[contains(text(), "Good")]')
It appears when we launch this URL, it doesn't load the whole page immediately.当我们启动这个 URL 时,它似乎不会立即加载整个页面。 As you scroll past, the subsequent elements started to load, which includes the vote section.
当您滚动过去时,后续元素开始加载,其中包括投票部分。 Also, the following
div
contains the buttons of interest:此外,以下
div
包含感兴趣的按钮:
<div class="button-group">
<button type="button" ...>👍 Good</button>
<button type="button" ...>👎 Bad</button>
</div>
And its class name button-group
appears to be unique.而它的 class 名称
button-group
似乎是独一无二的。 Searching for the element using CSS selector will negate any use of long XPATH strings.使用 CSS 选择器搜索元素将否定对 XPATH 长字符串的任何使用。
Accordingly, to achieve this, load the page, find document/window dimension for scrolling and scroll half through the document.因此,要实现这一点,请加载页面,找到用于滚动的文档/窗口尺寸并滚动一半文档。 Find the
div
and bring inxto view.找到
div
并将 inx 引入视图。 And with this div
, get the buttons and print their text.并使用此
div
获取按钮并打印其文本。 And then simply click the first button for Good .然后只需单击Good的第一个按钮。
Here is the code:这是代码:
def launch_find_vote_buttons(url):
# create webdriver object
chrome_srv = Service(driver_path)
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(service=chrome_srv,options=options)
driver.get(url)
# find doc/win height to get page count for scolling
doc_height = driver.execute_script("return document.body.scrollHeight")
win_height = driver.execute_script("return window.innerHeight")
num_pages = int(doc_height / win_height * 0.5)
print(f'doc height=>{doc_height}\tpages =>{num_pages}')
# scroll up to midway of the document
for page in range(num_pages):
driver.execute_script("window.scrollTo(0, arguments[0]);", win_height * (page+1))
print(f'\tscrolling to=>{win_height * (page+1)}')
sleep(2)
# find enclosing div and scroll to view
div_elem = driver.find_element(By.CSS_SELECTOR, 'div[class="button-group"]')
div_elem.location_once_scrolled_into_view
# find button within div and display their text
buttons = div_elem.find_elements(By.TAG_NAME,'button')
for i,btn in enumerate(buttons):
print(f'button {i} is {btn.text}')
# click Good button - commented :)
# buttons[0].click()
And here is a sample run:这是一个示例运行:
>>> launch_find_vote_buttons('https://coinmarketcap.com/currencies/bitcoin/')
doc height=>4288 pages =>3
scrolling to=>622
scrolling to=>1244
scrolling to=>1866
button 0 is 👍 Good
button 1 is 👎 Bad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.