簡體   English   中英

如何使用 Python-Selenium 修改源代碼?

[英]How can I modify source code using Python-Selenium?

我目前在 Python 上使用 Selenium 來自動化一些東西,我想做的是在檢查源代碼時修改它的特定部分。

具體來說,我想將以下代碼中的Blur(12px)替換為Blur(0px) ,以消除圖片的模糊性:

<div class="Bdrs(8px) Bgz(cv) Bgp(c) Ov(h) StretchedBox Ir(p) Cnt($blank)::a StretchedBox::a Bg($inherit)::a Scale(1.3)::a Scale(1.2)::a--s Blur(12px)::a" style="background-image: url(&quot;https://preview.(...).com/); background-position: 50% 100%; background-size: auto 100.952%;"></div>

我不太確定如何做到這一點,雖然我已經完成了我的研究,但我無法找到解決問題的適當方法。

您可以使用 javascript 來更改屬性。

element = driver.find_element_by_css_selector("... some css selector ...") # this can be however you want to find the element
driver.execute_script("arguments[0].setAttribute('class', 'Blur(0px)')", element)

這是假設您正在為您的 webdriver 對象使用變量“驅動程序”。

您提供的 html 示例在您的 class 屬性中有很多其他類。 如果您想保留其余部分,您可以先獲取屬性內容,將 Blur(12px) 替換為 Blur(0px),然后在 javascript 行中使用它作為您的值。

element = driver.find_element_by_css_selector("... some css selector ...") # this can be however you want to find the element
class_value = element.get_attribute("class")
revised_classes = class_value.replace("Blur(12px)", "Blur(0px)")
driver.execute_script("arguments[0].setAttribute('class', arguments[1])", element, revised_classes)

暫無
暫無

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

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